从ex读取数据时如何避免unicode问题

2024-10-02 22:37:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用dataframe从excel文件中读取数据。文本变为:

u"\u200bDuring the QA, bla bla bla,\xa0Head of bla bla\xa0for NZ,\xa0was labelled bla bal. With further investigation, bla bla bla bla bla bla."

我试图替换所有这些'u200b'、'\xa0'等,但仍然有一个“\'”无法替换,不知道为什么?你知道吗

def replaceMultiplePattern(row):             
    for r in ((u'\n', u''), (u'\xa0', u' '), (u'\u2019', "'"), (u'\u2013', '-'), (u'\u200b', ''), (u"\' ", u"'")):
        row = row.replace(*r)
    return row

另外,在从excel文件读取数据时,是否有任何方法可以避免所有这些unicode转换?你知道吗

谢谢


Tags: 文件ofthe文本dataframe读取数据qaexcel
2条回答

在python中加载excel后,可以使用unicodedata模块。 您也可以在保存excel文件时使用其编码。你知道吗

text= u"\u200bDuring the QA, bla bla bla,\xa0Head of bla bla\xa0for NZ,\xa0was labelled bla bal. With further investigation, bla bla bla bla bla bla."
from unicodedata import normalize 
t=normalize('NFD',text)
print(t)

您可能需要检查它们引用的documentation

Even if two unicode strings are normalized and look the same to a human reader, if one has combining characters and the other doesn’t, they may not compare equal.

也许在导入文件时添加编码是有效的。你知道吗

import pandas as pd
pd.read_excel('data.csv' encoding='utf-8')

相关问题 更多 >