Python Forum

Full Version: python csv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ?
We need a link to the imdb.txt.
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))
[Image: 33008433.png]

still not working...
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))
(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!
Glad to hear your code runs now ok.
(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 ?
Have you added error='replace' to line 5:


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