I have a comma seperated csv file "Brent 3.csv": Rows look like this:
2014.03.12 23:59:59,2014.03.20 23:59:59,BRENTSPOT,Brent
1,1.29,1.6,0.8568833439015613,91.09,3.5,2.053,-0.035\n
2014.04.01 23:59:59,2014.04.02 23:59:59,BRENTSPOT,Brent
1,1.39,1.4,0.8568833439015613,89.59999999999999,3.5,2.053,-0.036\n
Now what I wanted to do is to create a two dimensional mixed list with strings and float values.
import os
def create_list(stratname,directory):
os.chdir(directory)
temp = []
for file in glob.glob("*.csv"):
if stratname in file:
TDFile=open(file,"r")
for i,line in enumerate(TDFile):
s = line.split(',')
for x in s:
try:
temp.append(float(x))
except ValueError:
temp.append(x)
return temp
brent3 = create_list("Brent 3",strategydir)
print(brent3)
I know that I should use
temp.append([float(x)])
but that does only create a list that looks like that:
['2014.03.12 23:59:59', '2014.03.20 23:59:59', 'BRENTSPOT', 'Brent 1', [1.29], [1.6], [0.8568833439015613], [91.09], [3.5], [2.053], [-0.035], '2014.04.01 23:59:59', ... ]
It should be:
[['2014.03.12 23:59:59', '2014.03.20 23:59:59', 'BRENTSPOT', 'Brent 1', 1.29, 1.6, 0.8568833439015613, 91.09, 3.5, 2.053, -0.035], ['2014.04.01 23:59:59', .....]]
I just can't seem to find the answer how to get each line of the csv file in [[first line],[second line]] format.
Any help appreciated :)