简体   繁体   中英

UnicodeDecodeError when reading tsv file

I'm trying to read a tsv file into csv file but i keep getting the Unicodeerror. The code itself is correct i just don't know how to fix the error

import csv

# Open TSV file for reading
with open("data.tsv", "r") as titles:

    # Since the file is a TSV file, we can use the CSV reader and change
    # the separator to a tab.
    reader = csv.DictReader(titles, delimiter="\t")

    # Open new CSV file for writing
    with open("shows0.csv", "w") as shows:

        # Create writer
        writer = csv.writer(shows)

        # Write header of the columns we want
        writer.writerow(["tconst", "primaryTitle", "startYear", "genres"])

        # Iterate over TSV file
        for row in reader:

            # If non-adult TV show
            if row["titleType"] == "tvSeries" and row["isAdult"] == "0":

                # Write row
                writer.writerow([row["tconst"], row["primaryTitle"], row["startYear"], row["genres"]])
Traceback (most recent call last):
  File "c:\Users\ayoro\Desktop\python\import.py", line 20, in <module>
    for row in reader:
  File "C:\Python\Python39\lib\csv.py", line 111, in __next__
    row = next(self.reader)
  File "C:\Python\Python39\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1612: character maps to <undefined>

You should always specify the encoding in the open() method.

with open("shows0.csv", "w", encoding='utf-8') as shows:

...same for the input file, ofc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM