'ascii'编解码器不能编码字符错误 [英] 'ascii' codec can't encode character error
本文介绍了'ascii'编解码器不能编码字符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我请求您帮助解决错误。我试图使用Python将MS Access数据库表保存为CSV文件。我似乎遇到了一个错误,我不知道如何解决。我看过通过堆栈溢出不同的帖子,并尝试了,但没有任何履行。请提供您的帮助。
I request your kind assistance in tackling an error. I am trying to save MS Access database tables as CSV files using Python. I seem to be running into an error I do not know how to fix. I have looked through different posts on Stack overflow and tried them but nothing fulfilling. Please provide your kind assistance.
import pyodbc
import csv
conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Access\\permissions.accdb")
conn = pyodbc.connect(conn_string)
cursor = conn.cursor()
cursor.execute("select * from [Perm_Site Info];")
with open('C:\\Desktop\\Python Files\\Perms_Site_Info.csv','wb') as csvfile:
writer = csv.writer(csvfile)
rest_array = [text.encode("utf8") for text in cursor]
writer.writerow(rest_array)
writer.writerow([i[0] for i in cursor.description])
writer.writerows(cursor)
cursor.close()
conn.close()
print 'All done for now'
错误:
writer.writerows(cursor)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 4: ordinal not in range(128)
推荐答案
您应该安装并使用unicodecsv模块。
You should probably install and use the unicodecsv module.
https://pypi.python.org/pypi/unicodecsv/0.14.1
这篇关于'ascii'编解码器不能编码字符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文