在Python 3中将双反斜杠转换为单反斜杠 [英] Convering double backslash to single backslash in Python 3

查看:826
本文介绍了在Python 3中将双反斜杠转换为单反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

 >>> t 
'\\\\H\\\\e\\\\l\\\\l\\\\o\\\0000\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ u00b0'

我使用一个将unicode转换为代表性的Python转义序列的函数。然后,当我想要转换它,我不能摆脱双反斜杠,以便它被再次解释为unicode。如何做到这一点?

 >>> t = unicode_encode(
>>> t
'\\\\H\\\\e\\\\\\\\\\\\\\\\\\l\\\\o\\\\\\\\\\\\ u0020\\\\€\\\ \\\\°'
>>>> print(t)
\\\H\\\e\\\l\\\l\\\o \\\ \\\€\\\ \\\°
>>> t.replace('\\','X')
'Xu0048Xu0065Xu006cXu006cXu006fXu0020Xu20acXu0020Xu00b0'
> >> t.replace('\\','\\')
'\\\\H\\\\e\\\\l\\\\l\\ \\ u006f\\\\ \\\\\\\\\\\\\\\\\,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, p>当然,我也不能这样做:

 >>> t.replace(' \\\','\')
文件< ipython-input-155-b46c447d6c3d>,第1行
t.replace('\\','\')
^
SyntaxError:扫描字符串文字时的EOL


解决方案

不确定这是否适合您的情况,但您可以尝试使用 unicode_escape

 >>> t 
'\\\\H\\\\e\\\\l\\\\l\\\l\\\o\\\ \\\\\\\\ \\\\ \\\\°'
>>> type(t)
< class'str'>
>>> enc_t = t.encode('utf_8')
>>>> enc_t
b'\\\\H\\\\e\\\\l\\\\l\\\\o\\\o\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\ u00b0'
>>> type(enc_t)
< class'bytes'>
>>> dec_t = enc_t.decode('unicode_escape')
>>> type(dec_t)
< class'str'>
>>> dec_t
'你好€°'

或缩写形式:

 >>> t.encode('utf_8')。decode('unicode_escape')
'你好€?'

您使用 UTF-8 对字符串进行编码,然后使用 unicode_escape 对其进行解码。 p>

I have a string like so:

>>> t
'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'

That I made using a function that converts unicode to the representative Python escape sequences. Then, when I want to convert it back, I can't get rid of the double backslash so that it is interpreted as unicode again. How can this be done?

>>> t = unicode_encode("
>>> t
'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
>>> print(t)
\u0048\u0065\u006c\u006c\u006f\u0020\u20ac\u0020\u00b0    
>>> t.replace('\\','X')
'Xu0048Xu0065Xu006cXu006cXu006fXu0020Xu20acXu0020Xu00b0'
>>> t.replace('\\', '\\')
'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'

Of course, I can't do this, either:

>>> t.replace('\\', '\')
  File "<ipython-input-155-b46c447d6c3d>", line 1
    t.replace('\\', '\')
                         ^
SyntaxError: EOL while scanning string literal

解决方案

Not sure if this is appropriate for your situation, but you could try using unicode_escape:

>>> t
'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
>>> type(t)
<class 'str'>
>>> enc_t = t.encode('utf_8')
>>> enc_t
b'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
>>> type(enc_t)
<class 'bytes'>
>>> dec_t = enc_t.decode('unicode_escape')
>>> type(dec_t)
<class 'str'>
>>> dec_t
'Hello € °'

Or in abbreviated form:

>>> t.encode('utf_8').decode('unicode_escape')
'Hello € °'

You take your string and encode it using UTF-8, and then decode it using unicode_escape.

这篇关于在Python 3中将双反斜杠转换为单反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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