gpt4 book ai didi

python - 不使用 pandas 在 CSV 中创建新列表 : return UnicodeDecodeError

转载 作者:行者123 更新时间:2023-12-01 08:21:30 24 4
gpt4 key购买 nike

我正在尝试在现有的 csv 文件中创建一个新列表(不使用 pandas)。这是我的代码:

with open ('/Users/Weindependent/Desktop/dataset/albumlist.csv','r') as case0:
reader = csv.DictReader(case0)
album = []
for row in reader:
album.append(row)
print ("Number of albums is:",len(album))

CSV 文件是从 Rolling Stone's Top 500 albums data set on data.world 下载的。 .

我的逻辑是创建一个名为 album 的空列表,并包含此列表中的所有记录。但似乎 for row in reader 行有一些问题。

我收到的错误消息是:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 1040: invalid continuation byte

谁能告诉我我做错了什么?

最佳答案

您需要以正确的编解码器打开文件; UTF-8 不是正确的编码。数据集没有指定它,但我确定最有可能的编解码器是 mac_roman:

with open ('/Users/Weindependent/Desktop/dataset/albumlist.csv', 'r', encoding='mac_roman') as case0:

original Kaggle dataset懒得记录它,并且使用该集的各种内核都只是破坏编码。它显然是一个 8 位拉丁变体(大部分数据是 ASCII,带有一些单独的 8 位代码点)。

所以我分析了数据,发现9行中只有两个这样的代码点:

>>> import re
>>> eightbit = re.compile(rb'[\x80-\xff]')
>>> with open('albumlist.csv', 'rb') as bindata:
... nonascii = [l for l in bindata if eightbit.search(l)]
...
>>> len(nonascii)
9
>>> {c for l in nonascii for c in eightbit.findall(l)}
{b'\x89', b'\xca'}

0x89 字节仅出现在一行中:

>>> sum(l.count(b'\x89') for l in nonascii)
1
>>> sum(l.count(b'\xca') for l in nonascii)
22
>>> next(l for l in nonascii if b'\x89' in l)
b'359,1972,Honky Ch\x89teau,Elton John,Rock,"Pop Rock,\xcaClassic Rock"\r\n'

很明显Elton John's 1972 Honky Château album ,因此 0x89 字节必须代表 U+00E2 LATIN SMALL LETTER A WITH CIRCUMFLEX代码点。

0xCA 字节似乎都代表一个替代空格字符,它们都出现在流派和子流派列中的逗号后面(有一个专辑异常(exception)):

>>> import csv
>>> for row in csv.reader((l.decode('ascii', 'backslashreplace') for l in nonascii)):
... for col in row:
... if '\\' in col: print(col)
...
Reggae,\xcaPop,\xcaFolk, World, & Country,\xcaStage & Screen
Reggae,\xcaRoots Reggae,\xcaRocksteady,\xcaContemporary,\xcaSoundtrack
Electronic,\xcaStage & Screen
Soundtrack,\xcaDisco
Rock,\xcaBlues
Blues Rock,\xcaElectric Blues,\xcaHarmonica Blues
Garage Rock,\xcaPsychedelic Rock
Honky Ch\x89teau
Pop Rock,\xcaClassic Rock
Funk / Soul,\xcaFolk, World, & Country
Rock,\xcaPop
Stan Getz\xca/\xcaJoao Gilberto\xcafeaturing\xcaAntonio Carlos Jobim
Bossa Nova,\xcaLatin Jazz
Lo-Fi,\xcaIndie Rock

这些 0xCA 字节几乎肯定代表 U+00A0 NO-BREAK SPACE代码点。

通过这两个映射,您可以尝试确定哪些 8 位编解码器可以进行相同的映射。而不是手动尝试all Python's codecs我用过Tripleee's 8-bit codec mapping查看哪些编解码器使用这些映射。只有两个:

  • 0x89

â‎ (U+00E2): mac_arabic, mac_croatian, mac_farsi, mac_greek, mac_iceland, mac_roman, mac_romanian, mac_turkish

  • 0xca

    ‎ (U+00A0): mac_centeuro, mac_croatian, mac_cyrillic, mac_greek, mac_iceland, mac_latin2, mac_roman, mac_romanian, mac_turkish

两组中都列出了 6 种编码:

>>> set1 = set('mac_arabic, mac_croatian, mac_farsi, mac_greek, mac_iceland, mac_roman, mac_romanian, mac_turkish'.split(', '))
>>> set2 = set('mac_centeuro, mac_croatian, mac_cyrillic, mac_greek, mac_iceland, mac_latin2, mac_roman, mac_romanian, mac_turkish'.split(', '))
>>> set1 & set2
{'mac_turkish', 'mac_iceland', 'mac_romanian', 'mac_greek', 'mac_croatian', 'mac_roman'}

其中,Mac OS Roman mac_roman编解码器可能最有可能被用作 Microsoft Excel for Mac used Mac Roman to create CSV files许久。不过,这并不重要,这 6 个中的任何一个都可以在这里工作。

如果您想拆分流派和子流派列(如果取自 Discogs,则实际上是流派和风格列),您可能需要替换那些 U+00A0 不间断空格。

关于python - 不使用 pandas 在 CSV 中创建新列表 : return UnicodeDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54619140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com