我试图将一个tsv文件读入csv文件,但我一直收到Unicodeerror。代码本身是正确的,我只是不知道如何修复这个错误
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>
发布于 2021-03-11 19:53:12
您应该始终在open()
方法中指定编码。
with open("shows0.csv", "w", encoding='utf-8') as shows:
输入文件ofc的...same。
https://stackoverflow.com/questions/66581984
复制相似问题