Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python csv
#1
y the data for movies with a rank above 9 for a new file.

The dataset that im analyze contains rating on many movies obtained from IMDB.
The data fields are:

Votes: the number of people rating the movie
Rank: the averate rating of the movie
Title: the name of the movie
Year: the year in which the movie was released

the code i tried is:
 import csv
    
    filename = "IMDB.txt"
    with open(filename, 'rt', encoding='utf-8-sig') as imdb_file:
        imdb_reader = csv.DictReader(imdb_file, delimiter = '\t')
        with open('new file.csv', 'w', newline='') as high_rank:
            fieldnames = ['Votes', 'Rank', 'Title', 'Year']
            writer = csv.DictWriter(high_rank, fieldnames=fieldnames)
            writer.writeheader()
            for line_number, current_row in enumerate (imdb_reader):
                if(float(current_row['Rank']) > 9.0):
                    csv_writer.writerow(dict(current_row))

but unfortunately its not working, what should i do ?
Reply
#2
We need a link to the imdb.txt.
Reply
#3
https://www.dropbox.com/s/65y4pi9o0kczz98/IMDB.txt?dl=0
Reply
#4
Your code is ok, except last line.

#!/usr/bin/python3
import csv

filename = "IMDB.txt"
with open(filename, 'rt', encoding='utf-8-sig') as imdb_file:
   imdb_reader = csv.DictReader(imdb_file, delimiter = '\t')

   with open('new file.csv', 'w', newline='') as high_rank:
       fieldnames = ['Votes', 'Rank', 'Title', 'Year']
       writer = csv.DictWriter(high_rank, fieldnames=fieldnames)
       writer.writeheader()

       for line_number, current_row in enumerate (imdb_reader):
           if(float(current_row['Rank']) > 9.0):
               writer.writerow(dict(current_row))
Reply
#5
[Image: 33008433.png]

still not working...
Reply
#6
I add: errors=replace: (https://docs.python.org/3/library/functions.html#open)

#!/usr/bin/python3
import csv

filename = "IMDB.txt"
with open(filename, 'rt', encoding='utf-8-sig', errors='replace') as imdb_file:
   imdb_reader = csv.DictReader(imdb_file, delimiter = '\t')

   with open('new file.csv', 'w', newline='') as high_rank:
       fieldnames = ['Votes', 'Rank', 'Title', 'Year']
       writer = csv.DictWriter(high_rank, fieldnames=fieldnames)
       writer.writeheader()

       for line_number, current_row in enumerate (imdb_reader):
           if(float(current_row['Rank']) > 9.0):
               writer.writerow(dict(current_row))
Reply
#7
(May-29-2019, 05:49 PM)heiner55 Wrote: I add: errors=replace: (https://docs.python.org/3/library/functions.html#open)

#!/usr/bin/python3
import csv

filename = "IMDB.txt"
with open(filename, 'rt', encoding='utf-8-sig', errors='replace') as imdb_file:
   imdb_reader = csv.DictReader(imdb_file, delimiter = '\t')

   with open('new file.csv', 'w', newline='') as high_rank:
       fieldnames = ['Votes', 'Rank', 'Title', 'Year']
       writer = csv.DictWriter(high_rank, fieldnames=fieldnames)
       writer.writeheader()

       for line_number, current_row in enumerate (imdb_reader):
           if(float(current_row['Rank']) > 9.0):
               writer.writerow(dict(current_row))

i didn't understand what should i do with this error?
thanks for your help!
Reply
#8
Glad to hear your code runs now ok.
Reply
#9
(May-29-2019, 06:02 PM)heiner55 Wrote: Glad to hear your code runs now ok.

now i get a new problem

UnicodeEncodeError: 'charmap' codec can't encode character '\u01e1' in position 6: character maps to <undefined>


you know what is the problem now ?
Reply
#10
Have you added error='replace' to line 5:


with open(filename, 'rt', encoding='utf-8-sig', errors='replace') as imdb_file:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020