当前位置: 动力学知识库 > 问答 > 编程问答 >

Unicode string in python

问题描述:

I have

(Pdb) email

'\x00t\x00e\x00s\x00t\[email protected]\x00g\x00m\x00a\x00i\x00l\x00.\x00c\x00o\x00m\x00'

(Pdb) print email

[email protected]

I need to validate whether thie value is an email format, however, how can i convert this string to actual ascii string?

网友答案:

Seems like it's encoded with utf-16 encoding.

>>> '\x00t\x00e\x00s\x00t\[email protected]\x00g\x00m\x00a\x00i\x00l\x00.\x00c\x00o\x00m\x00'.decode('utf-16')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\encodings\utf_16.py", line 16, in decode
    return codecs.utf_16_decode(input, errors, True)
UnicodeDecodeError: 'utf16' codec can't decode byte 0x00 in position 28: truncated data

and truncated:

>>> '\x00t\x00e\x00s\x00t\[email protected]\x00g\x00m\x00a\x00i\x00l\x00.\x00c\x00o\x00m\x00'[1:].decode('utf-16')
u'[email protected]'

>>> '\x00t\x00e\x00s\x00t\[email protected]\x00g\x00m\x00a\x00i\x00l\x00.\x00c\x00o\x00m\x00'[1:].decode('utf-16-le')
u'[email protected]'
>>> '\x00t\x00e\x00s\x00t\[email protected]\x00g\x00m\x00a\x00i\x00l\x00.\x00c\x00o\x00m\x00'.decode('utf-16-be', 'ignore')
u'[email protected]'
分享给朋友:
您可能感兴趣的文章:
随机阅读: