OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Splitting a line in a .csv with Python

  • Thread starter Thread starter Guilherme
  • Start date Start date
G

Guilherme

Guest
I'm stuck with this part of code. I can't split a row in a .csv in Python. My code is:

Code:
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:

Code:
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...
<p>I'm stuck with this part of code. I can't split a row in a .csv in Python. My code is:</p>
<pre><code>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]
</code></pre>
<p>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:</p>
<pre><code>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
....
</code></pre>
<p>But for some reason it gives me this error:</p>
<blockquote>
<p>AttributeError: 'list' object has no attribute 'split'</p>
</blockquote>
<p>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...</p>
 

Latest posts

Top