首页 文章

如何使用pandas处理unicode字符并打印到屏幕?

提问于
浏览
1

我正在处理一个包含一系列unicode字符(勒索软件名称)的电子表格 .

目前我有以下内容:

import urllib.request
import pandas as pd

SOURCESHEET = 'https://docs.google.com/spreadsheets/d/1TWS238xacAto-fLKh1n5uTsdijWdCEsGIM0Y0Hvmc5g/pub?output=xlsx'
WORKBOOK = 'RansomwareOverview.xlsx'

# download and save ransomware overview file locally
try:
    urllib.request.urlretrieve(SOURCESHEET, WORKBOOK)
except IOError:
    print('An error occured trying to write an updated spreadsheet. Do you already have it open?')
except urllib.error.URLError:
    print('An error occured trying to download the file. Please check the source and try again')

sheet = pd.read_excel(open(WORKBOOK,'rb'), sheetname='Ransomware')
print(sheet)

当我尝试 print 表单的内容时,我收到以下内容:

Traceback(最近一次调用最后一次):文件“GoogleSpreadsheetToJson.py”,第27行,打印(工作表)文件“C:\ Python34 \ lib \ encodings \ cp850.py”,第19行,编码返回codecs.charmap_encode( input,self.errors,encoding_map)[0] UnicodeEncodeError:'charmap'编解码器不能编码位置10917-10922中的字符:字符映射到

我相信这是因为我正在使用的工作表具有以下属性:

“ПРОЧТИ_МЕНЯ.txtREAD_ME.txt”

有没有一种方法可以处理,或者在我的电子表格中使用pandas时将其解决?

1 回答

  • 2

    一些选择:

    • 切换到Python 3.6,它在您的系统上使用Windows ' Unicode APIs to write to the console instead of trying to encode to output in cp850 (the console'的默认编码) .

    • 使用 chcp 65001 (UTF-8)更改控制台编码 .

    • 在运行脚本之前设置环境变量 pythonioencoding=cp850:replace . 这会将错误处理程序从 strict 更改为 replace . 您将获得cp850不支持的字符的问号 .

相关问题