使用pyodbc将数据从excel导入python中的postgres
我正在使用 pyodbc
在 python(2.6)
中将数据从 MS-Excel 导入到 PostgreSQL。
面临的问题是:
Excel源代码中存在诸如左单引号(ANSI十六进制代码:0x91)
等字符。现在,当使用 pyodbc 将其导入到 PostgreSQL 时,它会终止并给出错误 DatabaseError: invalid byteequence for encoding "UTF8": 0x91
。
我尝试过的:我暂时使用了decode('unicode_escape')
。但是,这是无法完成的,因为这只是删除/转义相关字符。
替代试用:首先解码,到处使用 Unicode,然后在需要时从数据库进行编码。由于手头的项目规模庞大,这也无法完成。
请建议我一些方法/程序/内置函数来完成任务。
I am importing data from MS-Excel to PostgreSQL in python(2.6)
using pyodbc
.
The problem faced is:
There are characters like left single quotation mark(ANSI hex code : 0x91)
, etc in the excel source. Now, when it is import into PostgreSQL using pyodbc, it terminates and gives the error DatabaseError: invalid byte sequence for encoding "UTF8": 0x91
.
What I tried: I used decode('unicode_escape')
for the time being. But, this cannot be done as this simply removes/escapes the concerned character.
Alternate trial: Decode initially, Unicode everywhere and then Encode later when needed from database. This can also not be done due to the expanse of the project at hand.
Please suggest me some method/procedure/in-built functions to accomplish the task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找出源文件的真实编码。它可能是
WIN1251
。对其进行转码(例如使用 iconv)或设置client_encoding
PostgreSQL 相应地。如果您在 pyodbc 中没有设置(我不知道),您始终可以发出简单的 SQL 命令:
更多内容请参阅章节 “服务器和客户端之间的自动字符集转换” 手册。
Find out the real encoding of the source document. It might be
WIN1251
. Either transcode it (for instance with iconv) or set theclient_encoding
of PostgreSQL accordingly.If you don't have a setting in
pyodbc
(which I don't know), you can always issue a plain SQL command:More in the chapter "Automatic Character Set Conversion Between Server and Client" of the manual.