Hi, my friends,

I try to use csv module to input data in csv format but split does not work. Could you please help? The input data is 2 column data separated in comma as csv format.

The code is following:
reader = csv.reader(open('Indices.csv', 'r'), delimiter = ',')
line = 0
IndexHeader = reader.next()
line = line+1
for line in reader:
print line
TradingDate, MarketIndex = line.split(',')

The error is after running these code:
AttributeError: 'list' object has no attribute 'split'


The data is very simple like:
Date, Num
04/04/2006,1305.93000000
04/05/2006,1311.56000000
04/06/2006,1309.04000000
04/07/2006,1295.50000000
04/10/2006,1296.60000000
04/11/2006,1286.57000000


Thanks so much in advance.

John

Recommended Answers

All 6 Replies

You don't need split(). The reader() method returns a list already split. If you look at what print line outputs you should see: . So your code should read something like: TradingDate, MarketIndex = line[0], line[1] Of course you'll need to convert the data from string datatype to something more useful. You might wish to use decimal format for prices.

You don't need split(). The reader() method returns a list already split. If you look at what print line outputs you should see: . So your code should read something like: TradingDate, MarketIndex = line[0], line[1] Of course you'll need to convert the data from string datatype to something more useful. You might wish to use decimal format for prices.

Thanks so much. Yes. that works fine.

John

One more question: how can I convert a list of string, like here marketIndex to float?
Similarly, how can I concert a matrix of string into float type of data?

Thanks.

John

One more question: how can I convert a list of string, like here marketIndex to float?...

Easy, just use float() and a list comprehension:

>>> ohlc = ['1535.27','1543.89','1534.61','1542.92']
>>> flohlc = [float(dummy) for dummy in ohlc]
>>> print flohlc
[1535.27, 1543.8900000000001, 1534.6099999999999, 1542.9200000000001]

As far as a "matrix" goes, you'd have to specify how you're implementing a matrix. The same principles apply.

Easy, just use float() and a list comprehension:

>>> ohlc = ['1535.27','1543.89','1534.61','1542.92']
>>> flohlc = [float(dummy) for dummy in ohlc]
>>> print flohlc
[1535.27, 1543.8900000000001, 1534.6099999999999, 1542.9200000000001]

As far as a "matrix" goes, you'd have to specify how you're implementing a matrix. The same principles apply.

Hi, Thanks.

your code works in Pythonwin but fails when I put it in the script in easyEclipse for python:
The code is:
i =0
for row in reader:
i = i + 1
tradingDate, tem2 = row[0], row[1]

marketInex = [float(dummy) for dummy in tem2]

The erroe message is:
pydev debugger
Traceback (most recent call last):
File "C:\EasyEclipse-for-Python-1.2.2.2\plugins\org.python.pydev.debug_1.3.3\pysrc\pydevd.py", line 702, in <module>
debugger.run(setup, None, None)
File "C:\EasyEclipse-for-Python-1.2.2.2\plugins\org.python.pydev.debug_1.3.3\pysrc\pydevd.py", line 553, in run
execfile(file, globals, locals) #execute the script

Any idea what is wrong?

Thanks in advance.

John

Please use code tags when posting code.

Sorry, I don't know easyEclipse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.