python3用单反斜杠替换双反斜杠 [英] python3 replacing double backslash with single backslash

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

问题描述

我需要用 \ 替换 python3 中复杂字符串中的 \\ .我知道这个问题已经被问过好几次了,但大部分时间都是针对简单字符串的,因此所有(接受的)答案都不适用于复杂字符串.

这也不同这个的问题所在用 .decode('unicode_escape') 解决了这个问题.见下文.

假设字符串是:

my_str = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z\\X'

<小时>

直接的方法是:

my_str.replace('\\','\')

导致:

<块引用>

SyntaxError: EOL 扫描字符串文字

<小时>

答案建议使用:>

my_str.replace('\\\\','\\')

结果:

'\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z\\x'

所以,没有变化.

<小时>

这个答案表明:

b = bytes(my_str, encoding='utf-8')b.decode('unicode-escape')

但这对如此复杂的字符串不起作用:

<块引用>

UnicodeDecodeError: 'unicodeescape' 编解码器无法解码位置 49-50 中的字节:截断的 \xXX 转义

<小时>

使用解码(如建议的此处) 导致:

my_str.decode('unicode_escape')

<块引用>

AttributeError: 'my_str' 对象没有属性 'decode'

<小时>

使用unicode_esacpe 编码然后解码的组合返回一个完全不同的字符串(可能是由于使用了utf-16,但utf-8> 导致错误,见上文.另外,例如 latin1 不起作用):

my_str.encode('utf-16').decode('unicode_escape')'ÿþ\\\x00x\x00a\x005\x00\\\x00x\x00c\x000\x00\\\x00x\x00e\x006\x00a\x00K\x00\\\x00x\x00f\x009\x00x\\x008\x000\x00\\\x00x\x00b\x001\x00\\\x00x\x00c\x008\x00*\x00\x01\x00\x12\x00$\x00\\\x00x\x00f\x00b\x\x00\x1e\x00(\x004\x00\\\x00x\x00d\x006\x00{\x00;\x00Z\x00\\\x00x\x00'

<小时>

解决方案

仔细看字符串,都是单斜杠.

在 [26] 中:my_str[0]出 [26]: '\\'在 [27]: my_str[1]出 [27]: 'x'在 [28]: len(my_str[0])出[28]:1

而且 my_str.replace('\\','\') 不起作用,因为这里的标记是 \',它转义了 ' 并等待另一个关闭的 '.
使用 my_str.replace('\\', '') 代替

<小时>

更新:几天后,我意识到以下讨论也可能有所帮助.如果带有转义符('\\x''\\u')的字符串的内涵最终是十六进制/Unicode 文字,它们可以被 解码escape_decode.

导入编解码器print(len(b'\x32'), b'\x32') # 1 十六进制文字,'\x32' == '2'print(len(b'\\x32'), b'\\x32') # 4 个字符,包括转义符print(codecs.escape_decode('\\x32', 'hex')) # chars->literal, 4->1# 1 b'2'# 4 b'\\x32'# (b'2', 4)s = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z'ed, _ = codecs.escape_decode(s, 'hex')打印(镜头(S),S)打印(len(ed),ed)# 49 \xa5\xc0\xe6aK\xf9\x80\xb1\xc8*$\xfbp(4\xd6{;Z# 22 b'\xa5\xc0\xe6aK\xf9\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'

I need to replace \\ with \ in python3 in a complex string. I know that this question had been asked several times, but most of the time for simple strings so that none of the (accepted) answers really works for complex strings.

This is also different from this one where the problem could be solved with .decode('unicode_escape') which does not work for this problem. See below.

Assuming the string is:

my_str = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z\\x'


Straight forward approach would be:

my_str.replace('\\','\')

which leads to:

SyntaxError: EOL while scanning string literal


This answer suggests using:

my_str.replace('\\\\','\\')

Which results in:

'\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z\\x'

So, there is no change.


This answer suggests:

b = bytes(my_str, encoding='utf-8')
b.decode('unicode-escape')

But this doesn't work for such a complex string:

UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 49-50: truncated \xXX escape


Using decode (as suggested here) results in:

my_str.decode('unicode_escape')

AttributeError: 'my_str' object has no attribute 'decode'


A combination of encoding and then decoding using unicode_esacpe returns a totally different string (probably due to using utf-16, but utf-8 results in an error, see above. Also, e.g. latin1 doesn't work):

my_str.encode('utf-16').decode('unicode_escape')
'ÿþ\\\x00x\x00a\x005\x00\\\x00x\x00c\x000\x00\\\x00x\x00e\x006\x00a\x00K\x00\\\x00x\x00f\x009\x00\\\x00x\x008\x000\x00\\\x00x\x00b\x001\x00\\\x00x\x00c\x008\x00*\x00\x01\x00\x12\x00$\x00\\\x00x\x00f\x00b\x00p\x00\x1e\x00(\x004\x00\\\x00x\x00d\x006\x00{\x00;\x00Z\x00\\\x00x\x00'


解决方案

Take a closer look at the string, they are all single slash.

In [26]: my_str[0]
Out[26]: '\\'

In [27]: my_str[1]
Out[27]: 'x'

In [28]: len(my_str[0])
Out[28]: 1

And my_str.replace('\\','\') won't work because the token here is \', which escapes ' and waits for the another closing '.
Use my_str.replace('\\', '') instead


Update: after few more days, I realize the following discussion may also be helpful. If the intension of a string with escape ('\\x' or '\\u') are eventually hex/unicode literals, they can be decoded by escape_decode.

import codecs
print(len(b'\x32'), b'\x32')                # 1 hex literal, '\x32' == '2'
print(len(b'\\x32'), b'\\x32')              # 4 chars including escapes
print(codecs.escape_decode('\\x32', 'hex')) # chars->literal, 4->1

# 1 b'2'
# 4 b'\\x32'
# (b'2', 4)

s = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z'
ed, _ = codecs.escape_decode(s, 'hex')
print(len(s), s)
print(len(ed), ed)

# 49 \xa5\xc0\xe6aK\xf9\x80\xb1\xc8*$\xfbp(4\xd6{;Z
# 22 b'\xa5\xc0\xe6aK\xf9\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'

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

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