问题描述
我正在尝试将用户帐户数据从 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 使用以下示例格式返回一个字符串:Mxc3xbcller
The Active Directory returns a string using this sample format: Mxc3xbcller
这实际上是 Müller
的 UTF-8 编码,但我想将 Müller
写入我的数据库而不是 Mxc3xbcller
.
This actually is the UTF-8 encoding for Müller
, but I want to write Müller
to my database not Mxc3xbcller
.
我尝试用这一行转换字符串,但它在数据库中产生了相同的字符串: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 "Mxc3xbcller".decode("utf-8")
输出是正确的.
If I run print "Mxc3xbcller".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')
解码字符串终于成功了.现在一切都按原样插入了.完整的其他解决方案可以在这里找到:Working通过 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!