使用pyodbc将数据从excel导入python中的postgres [英] import data from excel to postgres in python using pyodbc

查看:50
本文介绍了使用pyodbc将数据从excel导入python中的postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pyodbcpython(2.6) 中将数据从 MS-Excel 导入到 PostgreSQL.

I am importing data from MS-Excel to PostgreSQL in python(2.6) using pyodbc.

面临的问题是:

excel源码中有左单引号(ANSI hex code : 0x91)等字符.现在,当它使用 pyodbc 导入 PostgreSQL 时,它会终止并给出错误 DatabaseError: invalid byte sequence for encoding "UTF8": 0x91.

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.

我的尝试:我暂时使用了 decode('unicode_escape').但是,这不能完成,因为这只是删除/转义相关字符.

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.

替代试验: 最初解码,Unicode 无处不在,然后在需要时从数据库中编码.由于手头的工程量很大,这也无法做到.

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.

推荐答案

找出源文档的真实编码.它可能是 WIN1251.对其进行转码(例如使用 iconv)或设置 client_encoding相应地使用 PostgreSQL.

Find out the real encoding of the source document. It might be WIN1251. Either transcode it (for instance with iconv) or set the client_encoding of PostgreSQL accordingly.

如果你在 pyodbc 中没有设置(我不知道),你总是可以发出一个普通的 SQL 命令:

If you don't have a setting in pyodbc (which I don't know), you can always issue a plain SQL command:

SET CLIENT_ENCODING TO 'WIN1251';

服务器和客户端之间的自动字符集转换"一章中的更多内容 手册.

这篇关于使用pyodbc将数据从excel导入python中的postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆