简体   繁体   English

Python 2.7 - 带有来自 pyodbc 的数据的 Pandas UnicodeEncodeError

[英]Python 2.7 - Pandas UnicodeEncodeError with data from pyodbc

I'm trying to pull data from SQL Server using pyodbc and load it into a dataframe, then export it to an HTML file, except I keep receiving the following Unicode error:我正在尝试使用 pyodbc 从 SQL Server 中提取数据并将其加载到数据帧中,然后将其导出到 HTML 文件,但我一直收到以下 Unicode 错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 15500: ordinal not in range(128)

Here is my current setup (encoding instructions per docs ):这是我当前的设置(每个文档的编码说明):

cnxn =  pyodbc.connect('DSN=Planning;UID=USER;PWD=PASSWORD;')
cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='cp1252', to=unicode)
cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='cp1252', to=unicode)
cnxn.setdecoding(pyodbc.SQL_WMETADATA, encoding='cp1252', to=unicode)
cnxn.setencoding(str, encoding='utf-8')
cnxn.setencoding(unicode, encoding='utf-8')
cursor = cnxn.cursor()

with open('Initial Dataset.sql') as f:
    initial_query = f.read()

cursor.execute(initial_query)
columns = [column[0] for column in cursor.description]
initial_data = cursor.fetchall()
i_df = pd.DataFrame.from_records(initial_data, columns=columns)
i_df.to_html('initial.html')

An odd but useful point to note is that when I try to export a CSV:一个奇怪但有用的注意点是,当我尝试导出 CSV 时:

i_df.to_csv('initial.csv')

I get the same error, however when I add:我得到同样的错误,但是当我添加:

i_df.to_csv('initial.csv', encoding='utf-8')

It works.有用。 Can someone help me understand this encoding issue?有人可以帮我理解这个编码问题吗?

Side note: I've also tried using a sqlalchemy connection and pandas.read_sql() and the same error persists.附注:我使用也尝试sqlalchemy连接和pandas.read_sql()和相同的错误仍然存在。

The second answer on this question seems to be an acceptable workaround, except for Python 2.x users, you must use io , so:关于这个问题的第二个答案似乎是一个可以接受的解决方法,除了 Python 2.x 用户,你必须使用io ,所以:

import io

html = df.to_html()
with io.open("mypage.html", "w", encoding="utf-8") as file:
    file.write(html)

It was not included in the latest release, but it looks like the next version of pandas will have an encoding option for to_html() , see docs (line 2228).它没有包含在最新版本中,但看起来下一版本的pandas将具有to_html()encoding选项,请参阅文档(第 2228 行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM