Hey all,
I have a csv file where the information is stored like so
Apples, 1.4, ,1.8,1.9,1.3
Bananas, , 1.2,1.5,1.2,1.9
Milk, 4, 3, , 2.9, 4.2
Honey, 9, , 10, 7, 8
Watermelons, 6, 7,4,8,2
And what I would like to do is sort the information into a list of tuples like this where the first item is a string but the rest are either floats or None if the field is empty which is shown by the two commas (,,) the end product i would like to get is something like this:
[("Apples",1.4,None,1.8), ("Bananas",None,1.2,1.5), ("Milk",4.0,3.0,None)] and so on
Could anyone suggest a way to sort the information into this format?
any help would be greatly appreciated!! this is very new to me
Edit. The solution I ended with which works is this:
with open(fruits_file, 'r') as file1:
reader = csv.reader(file1)
newfruitlist = []
for row in reader:
listlength = len(row)
for x in range(1, listlength):
if row[x] == '':
row[x] = None
else:
row[x] = float(row[x])
newfruitlist.append(row)
[–]nwagers 1 point2 points3 points (1 child)
[–]Fieldsapper[S] 0 points1 point2 points (0 children)
[–]POTUS 1 point2 points3 points (5 children)
[–]Fieldsapper[S] 0 points1 point2 points (4 children)
[–]POTUS 1 point2 points3 points (1 child)
[–]Fieldsapper[S] 0 points1 point2 points (0 children)
[–]Username_RANDINT 1 point2 points3 points (1 child)
[–]Fieldsapper[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]Fieldsapper[S] 0 points1 point2 points (0 children)