在Python 3.7中编码、解码拼音字符

问题描述 投票:0回答:1

我在对一个excel文件的拼音字符进行解码时遇到了问题。

该文件的结构是这样的。

enter image description here

[...]

我想把每一列的值存储在一个单独的变量中,这样做是: (对于拼音,我需要通过 漠视 争论)

df = pd.read_excel("static\\voc1.xlsx")

#English doesn't actually need en-/decoding
en = str(df['English'][:3]).encode('utf-8').decode('cp1252')
zh = str(df['Chinese'][:3]).encode('utf-8').decode('cp1252')
pinyin = str(df['Pinyin'][:3]).encode('utf-8').decode('cp1252', 'ignore')

print(zh, en, pinyin)

结果在执行时出现了以下情况。

0     我
1    我们
2     你
Name: Chinese, dtype: object
0     I, me
1    we, us
2       you
Name: English, dtype: object
0       wǒ
1    wǒmen
2       n�
Name: Pinyin, dtype: object

正如你所看到的,这对汉字和英文都有效,但对拼音来说,它要么在某些字符上显示 "钻石问号",要么就是在我没有传递一个... 错误 参数。

df = pd.read_excel("static\\voc1.xlsx")

#English doesn't actually need en-/decoding
en = str(df['English'][:3]).encode('utf-8').decode('cp1252')
zh = str(df['Chinese'][:3]).encode('utf-8').decode('cp1252')
pinyin = str(df['Pinyin'][:3]).encode('utf-8').decode('cp1252')

print(zh, en, pinyin)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 34: character maps to <undefined>

如果你想自己复制: 我在这里使用这个文件。https:/www.mandarinzone.comwp-contentuploads201904HSK1-Vocabulary-List.xlsx

在Win10 64位上使用Visual Studio Code

非常感谢您的帮助

python-3.x pandas encoding utf-8 decoding
1个回答
0
投票

终于明白了问题所在.我是通过VS'Code Runner'扩展运行的,导致了错误。我想可以通过某种方式配置它来使其工作,但目前我只是在VS Code中使用标准终端或直接通过CLI运行它。

© www.soinside.com 2019 - 2024. All rights reserved.