Pandas Dataframe与文本和一些字符的编码问题。

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

我有一个数据集,其中包括一列文字(歌词)。

有时在文本中会有一些单词(或符号)没有被正确解码,这里是一个例子。

'I keep trying Ainâ\\x80\\x99t no denyingWe should be together nowI canâ\\x80\\x99t imagineYouâ\\x80\\x99re with another man Baby'

我想用regex找到所有这些子串,但我不知道模式是否相同(一个字母,两个反斜杠,x加上两个数字)

还是有一些简单的编码参数,"读取 "所有的字符?

谢谢你的帮助!我有一个数据集,其中包括一列文本(歌词)。

python regex pandas character-encoding decoding
1个回答
1
投票

如果我的问题是正确的,你需要找到正确的文件编码。

像这样找到文件的编码。

# import the chardet library
import chardet 

# use the detect method to find the encoding
# 'rb' means read in the file as binary
with open(your_file, 'rb') as file:
    print(chardet.detect(file.read()))

这个片段将打印出正确的文件编码,如下图:

{'encoding': 'UTF-16', 'confidence': 1.0, 'language': ''}

现在用正确的编码打开你的文件。

如果你没有安装chardet库。

pip install chardet

希望这能帮助你。

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