在python 2中用十六进制字符解码字符串
我有一个十六进制字符串,我想将其转换为utf8以插入mysql。 (我的数据库是utf8)
hex_string = 'kitap ara\xfet\xfdrmas\xfd'
...
result = 'kitap araştırması'
我该怎么做?
I have a hex string and i want to convert it utf8 to insert mysql. (my database is utf8)
hex_string = 'kitap ara\xfet\xfdrmas\xfd'
...
result = 'kitap araştırması'
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试(Python 3.x):
来自此处。
Try(Python 3.x):
From here.
假设Python 2.6,
Assuming Python 2.6,
字符串文字 解释了如何在 Python 源代码中使用 UTF8 字符串。
String literals explains how to use UTF8 strings in Python source.
尝试
(
cp1254
或iso-8859-9
是土耳其语代码页,前者是 Windows 平台上的常用名称,但在 Python 中,两者都同样有效)Try
(
cp1254
oriso-8859-9
are the Turkish codepages, the former being the usual name on Windows platforms, but in Python, both work equally well)首先,您需要从您拥有的编码字节中对其进行解码。这似乎是 ISO-8859-9 (latin-5),或者,如果您使用的是 Windows,可能是 代码页 1254,基于 latin-5。
如果您使用Windows,那么根据您获取这些字节的位置,可能将它们解码为
mbcs
更合适,它翻译为到“本地系统正在使用的代码页”。如果字符串仅位于.py
文件中,则最好在源代码中编写u'kitap araştırması'
并设置-*- coding
声明来指导 Python 对其进行解码。请参阅 PEP 263。至于如何将数据库的 unicode 字符串编码为 UTF-8,如果您愿意,您可以手动执行:
但是如果您有
COLLATION。
First you need to decode it from the encoded bytes you have. That appears to be ISO-8859-9 (latin-5), or, if you are using Windows, probably code page 1254, which is based on latin-5.
If you are using Windows, then depending on where you are getting those bytes, it might be more appropriate to decode them as
mbcs
, which translates to ‘whichever code page the local system is using’. If the string is just sitting in a.py
file, you would be better off just writingu'kitap araştırması'
in the source and setting a-*- coding
declaration to direct Python to decode it. See PEP 263.As to how to encode unicode strings to UTF-8 for the database, well, if you want to you can do it manually:
but a good data access layer is likely to do that automatically for you, if you've got the
COLLATION
of the tables the data is going into right.