简体   繁体   English

'ascii'编解码器无法编码字符错误

[英]'ascii' codec can't encode character error

I request your kind assistance in tackling an error. 我请求你帮助解决错误。 I am trying to save MS Access database tables as CSV files using Python. 我试图使用Python将MS Access数据库表保存为CSV文件。 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. 我已经查看了Stack溢出的不同帖子并尝试了它们但没有任何实现。 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'

The error: 错误:

writer.writerows(cursor)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 4: ordinal not in range(128)

You should probably install and use the unicodecsv module. 您应该安装并使用unicodecsv模块。

https://pypi.python.org/pypi/unicodecsv/0.14.1 https://pypi.python.org/pypi/unicodecsv/0.14.1

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

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