使用Python将UTF-8字符串写入MySQL [英] Writing UTF-8 String to MySQL with Python

查看:268
本文介绍了使用Python将UTF-8字符串写入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户帐户数据从Active Directory推送到我们的MySQL服务器.可以完美地运行,但是以某种方式最终使字符串显示了变音符号和其他特殊字符的编码版本.

I am trying to push user account data from an Active Directory to our MySQL-Server. This works flawlessly but somehow the strings end up showing an encoded version of umlauts and other special characters.

Active Directory使用以下示例格式返回一个字符串:M\xc3\xbcller

The Active Directory returns a string using this sample format: M\xc3\xbcller

这实际上是Müller的UTF-8编码,但是我想将Müller写入数据库而不是M\xc3\xbcller.

This actually is the UTF-8 encoding for Müller, but I want to write Müller to my database not M\xc3\xbcller.

我尝试使用此行转换字符串,但是它导致数据库中的字符串相同: tempEntry[1] = tempEntry[1].decode("utf-8")

I tried converting the string with this line, but it results in the same string in the database: tempEntry[1] = tempEntry[1].decode("utf-8")

如果我在python控制台中运行print "M\xc3\xbcller".decode("utf-8"),则输出正确.

If I run print "M\xc3\xbcller".decode("utf-8") in the python console the output is correct.

有什么方法可以正确插入此字符串?对于需要具有这种确切格式的Web开发人员,我需要这种特定格式,但我不知道为什么他不能直接使用PHP转换字符串.

Is there any way to insert this string the right way? I need this specific format for a web developer who wants to have this exact format, I don't know why he is not able to convert the string using PHP directly.

其他信息:我正在使用MySQLdb;表和列的编码为utf8_general_ci

Additional info: I am using MySQLdb; The table and column encoding is utf8_general_ci

推荐答案

我找到了解决问题的方法.最后,用.decode('unicode_escape').encode('iso8859-1').decode('utf8')解码了字符串.现在,所有内容均已插入.完整的其他解决方案可以在这里找到:工作通过python-ldap从Active Directory中使用unicode编码的字符串

I found the solution to my problems. Decoding the String with .decode('unicode_escape').encode('iso8859-1').decode('utf8') did work at last. Now everything is inserted as it should. The full other solution can be found here: Working with unicode encoded Strings from Active Directory via python-ldap

这篇关于使用Python将UTF-8字符串写入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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