【问题标题】:How to handle unicode characters with pandas and printing to screen?如何使用 pandas 处理 unicode 字符并打印到屏幕?
【发布时间】:2017-07-18 19:33:41
【问题描述】:

我正在处理一个包含一系列 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 行,在编码中 return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in 位置 10917-10922:字符映射到

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

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

有什么方法可以在我的电子表格中仍然使用 pandas 的同时处理或逾越这个问题吗?

【问题讨论】:

  • 你的终端支持unicode吗?
  • @juanpa.arrivillaga 我没有考虑过。我现在将在 bash 上对其进行测试并更新...
  • 当我尝试 print 到以前不支持 unicode 的终端时,我看到了 UnicodeEncodeError...

标签: python excel pandas unicode


【解决方案1】:

一些选项:

  1. 切换到 Python 3.6,它使用 Windows 的 Unicode API 写入控制台,而不是尝试以 cp850(系统上控制台的默认编码)编码输出。
  2. 使用chcp 65001 (UTF-8) 更改控制台编码。
  3. 在运行脚本之前设置环境变量pythonioencoding=cp850:replace。这会将错误处理程序从 strict 更改为 replace。您会得到 cp850 不支持的字符的问号。

【讨论】:

  • Python 3.4...如此接近!我会更新和拉。感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多