I'm stuck with this part of code. I can't split a row in a .csv in Python. My code is:
import csv
# Get movie titles
movies={}
with open(path+'/ratings.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
(Id,title)=row.split(',')[0:1]
So I want to get the Id of the movie, which is the first value, and them the title which is the second value. The .csv file looks like this:
userId,movieId,rating,timestamp
1,31,2.5,1260759144
1,1029,3.0,1260759179
1,1061,3.0,1260759182
1,1129,2.0,1260759185
1,1172,4.0,1260759205
1,1263,2.0,1260759151
1,1287,2.0,1260759187
1,1293,2.0,1260759148
1,1339,3.5,1260759125
1,1343,2.0,1260759131
1,1371,2.5,1260759135
1,1405,1.0,1260759203
1,1953,4.0,1260759191
....
But for some reason it gives me this error:
AttributeError: 'list' object has no attribute 'split'
When I searched, others were having a similar problem, but mostly because they were .split the file is not the line, but I'm splitting the line...
row is already a list, that's the point of the csv module (since you specified the comma as a separator, which you could have omitted because it's default)
Just do:
Id,title = row[0:1]
BTW to skip title line just do next(reader) before the for loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With