开发者

Python file input string: how to handle escaped unicode characters?

开发者 https://www.devze.com 2022-12-29 18:08 出处:网络
In a text file (test.txt), my string looks like this: Gro\\u00DFbritannien Reading it, python escapes the backslash:

In a text file (test.txt), my string looks like this:

Gro\u00DFbritannien

Reading it, python escapes the backslash:

>>> file = open('test.txt', 'r')
>>> input = file.readline()
>>> input
'Gro\\u00DFbritannien'

How can I have this interpreted as unicode? decode() and unicode() won't do the job.

The following code writes Gro\u00DFbritannien back to the file,开发者_如何学Python but I want it to be Großbritannien

>>> input.decode('latin-1')
u'Gro\\u00DFbritannien'
>>> out = codecs.open('out.txt', 'w', 'utf-8')
>>> out.write(input)


You want to use the unicode_escape codec:

>>> x = 'Gro\\u00DFbritannien'
>>> y = unicode(x, 'unicode_escape')
>>> print y
Großbritannien

See the docs for the vast number of standard encodings that come as part of the Python standard library.


Use the built-in 'unicode_escape' codec:

>>> file = open('test.txt', 'r')
>>> input = file.readline()
>>> input
'Gro\\u00DFbritannien\n'
>>> input.decode('unicode_escape')
u'Gro\xdfbritannien\n'

You may also use codecs.open():

>>> import codecs
>>> file = codecs.open('test.txt', 'r', 'unicode_escape')
>>> input = file.readline()
>>> input
u'Gro\xdfbritannien\n'

The list of standard encodings is available in the Python documentation: http://docs.python.org/library/codecs.html#standard-encodings

0

精彩评论

暂无评论...
验证码 换一张
取 消