使用pyodbc将数据从excel导入python中的postgres

发布于 2024-12-17 14:22:17 字数 554 浏览 2 评论 0原文

我正在使用 pyodbcpython(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2024-12-24 14:22:17

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

如果您在 pyodbc 中没有设置(我不知道),您始终可以发出简单的 SQL 命令:

SET CLIENT_ENCODING TO 'WIN1251';

更多内容请参阅章节 “服务器和客户端之间的自动字符集转换” 手册。

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.

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';

More in the chapter "Automatic Character Set Conversion Between Server and Client" of the manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文