Python psycopg2 不在 utf-8 中 [英] Python psycopg2 not in utf-8

查看:71
本文介绍了Python psycopg2 不在 utf-8 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python 连接到我的 postgresql 数据库,如下所示:

I use Python to connect to my postgresql data base like this:

conn=psycopg2.connect(database="fedour", user="fedpur", password="***", host="127.0.0.1", port="5432")

没问题.

但是当我进行查询并想打印光标时,我有这样的事情:

But when I make a query and I want to print the cursor I have something like this:

Fran\xc3\xa7ois"而不是François",当我想用​​它创建一个 XML 文档时它会导致问题.我认为来自我的编码,但我找到了任何解决方案.我尝试编码('utf-8')但不起作用.

"Fran\xc3\xa7ois" instead of "François" and it cause problem when I want to create a XML document with this. I thkink is come from my encodage, but I found any solution. I try to encode('utf-8') but doesn't work.

我也看到过类似的东西,但它只适用于 mySQL

I have also seen something like this, but its only for mySQL

MySQLdb.connect(charset='utf8', init_command='SET NAMES UTF8')

你能帮我吗?谢谢

推荐答案

通过运行:print conn.encoding 确保您使用了正确的编码,如果需要,您可以设置通过 conn.set_client_encoding('UNICODE')conn.set_client_encoding('UTF8') 正确编码.

Make sure you're using the right encodind by running: print conn.encoding, and if you need, you can set the right encoding by conn.set_client_encoding('UNICODE'), or conn.set_client_encoding('UTF8').

这篇关于Python psycopg2 不在 utf-8 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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