相关疑难解决方法(0)

如何在Python中转换转义字符?

我想将包含转义字符的字符串转换为它们的正常形式,就像Python的词法解析器一样:

>>> escaped_str = 'One \\\'example\\\''
>>> print(escaped_str)
One \'Example\'
>>> normal_str = normalize_str(escaped_str)
>>> print(normal_str)
One 'Example'
Run Code Online (Sandbox Code Playgroud)

当然,无聊的方法是逐个替换所有已知的转义字符:http: //docs.python.org/reference/lexical_analysis.html#string-literals

您将如何normalize_str()在上面的代码中实现?

python string-formatting

6
推荐指数
3
解决办法
2万
查看次数

在运行时删除 Python 中的反斜杠

我需要一种方法让我的函数在运行时接收一个字符串并删除反斜杠,同时保留它前面的字符。所以对于 \a 我必须得到一个。这也必须适用于像 \e -> e 这样的非转义字符。

我已经在互联网上寻找解决此问题的一般解决方案,但似乎没有。我发现的最佳解决方案是使用字典从头开始构建字符串,例如:如何防止 Python 中的特殊字符自动转义

escape_dict={'\a':r'\a',
         '\b':r'\b',
         '\c':r'\c',
         '\f':r'\f',
         '\n':r'\n',
         '\r':r'\r',
         '\t':r'\t',
         '\v':r'\v',
         '\'':r'\'',
         '\"':r'\"',
         '\0':r'\0',
         '\1':r'\1',
         '\2':r'\2',
         '\3':r'\3',
         '\4':r'\4',
         '\5':r'\5',
         '\6':r'\6',
         '\7':r'\7',
         '\8':r'\8',
         '\9':r'\9'}
def raw(text):
    """Returns a raw string representation of the string"""
    new_string=''
    for char in text:
        try: 
            new_string += escape_dict[char]
        except KeyError: 
            new_string += char
    return new_string
Run Code Online (Sandbox Code Playgroud)

然而,由于转义数字和转义字母之间的冲突,这通常会失败。使用像 \001 而不是 \1 这样的 3 位数字也会失败,因为输出中会有额外的数字,这违背了目的。我应该简单地删除反斜杠。其他基于编码的建议解决方案,例如在 Python 中处理字符串中的转义序列

也不起作用,因为这只是将转义字符转换为十六进制代码。\a 被转换为 \x07。即使以某种方式删除它,字符 a 仍然丢失。

python string character-encoding

6
推荐指数
1
解决办法
124
查看次数

我如何在python中浏览unicode转义字符串?

Python 3.4

我有一个unicode转义字符串:

> str = 'blah\\x2Ddude'
Run Code Online (Sandbox Code Playgroud)

我想将此字符串转换为unicode非转义版本 'blah-dude'

我该怎么做呢?

python escaping python-3.x python-3.4

5
推荐指数
1
解决办法
3502
查看次数

将argparse转义字符作为选项处理

argparse库对转义字符(如\ t到制表符和\ n到换行符)的处理方式与我更喜欢。该问题的答案给出了解决方案,但我想使它对用户不可见。

给定程序:

#!/usr/bin/env python3
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--delimiter', default='\t')
args = parser.parse_args()
print(args)
Run Code Online (Sandbox Code Playgroud)

您将收到以下输出:

bash$ parser.py -d \t
Namespace(delimiter='t')

bash$ parser.py -d \\t
Namespace(delimiter='\\t')

bash$ parser.py -d '\t'
Namespace(delimiter='\\t')

bash$ parser.py -d '\\t'
Namespace(delimiter='\\\\t')

bash$ parser.py -d "\t"
Namespace(delimiter='\\t')

bash$ parser.py -d "\\t"
Namespace(delimiter='\\t')

bash$ parser.py -d $'\t'
Namespace(delimiter='\t')

bash$ parser.py -d $'\\t'
Namespace(delimiter='\\t')

bash$ parser.py -d $"\t"
Namespace(delimiter='$\\t')

bash$ parser.py -d $"\\t"
Namespace(delimiter='$\\t')
Run Code Online (Sandbox Code Playgroud)

我只有用

parser.py -d $'\t'
Run Code Online (Sandbox Code Playgroud)

但我希望输入看起来像

parser.py -d \t 
Run Code Online (Sandbox Code Playgroud)

或更小 …

python delimiter argparse

5
推荐指数
2
解决办法
2772
查看次数

逆转Python的re.escape

如何反向重新逃生?这个2007年的博客说没有反向功能,但是十年后仍然如此吗?

Python 2 decode('string_escape')不适用于所有转义的字符(例如空格)。

>>> re.escape(' ')
'\\ '
>>> re.escape(' ').decode('string-escape')
'\\ '
Run Code Online (Sandbox Code Playgroud)

Python的3:有些人建议 unicode_escapecodec.escape_decodeast.literal_eval但空间没有运气。

>>> re.escape(b' ')
b'\\ '
>>> re.escape(b' ').decode('unicode_escape')
'\\ '
>>> codecs.escape_decode(re.escape(b' '))
(b'\\ ', 2)
>>> ast.literal_eval(re.escape(b' '))
ValueError: malformed node or string: b'\\ '
Run Code Online (Sandbox Code Playgroud)

那么这真的是唯一有效的方法吗?

>>> re.sub(r'\\(.)', r'\1', re.escape(' '))
' '
Run Code Online (Sandbox Code Playgroud)

python regex escaping python-3.x

5
推荐指数
1
解决办法
494
查看次数

python3用单反斜杠替换双反斜杠

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

这也是不同的,从这个地方的问题可以用来解决.decode('unicode_escape')这对于这个问题无法正常工作.见下文.

假设字符串是:

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

直接的方法是:

my_str.replace('\\','\')
Run Code Online (Sandbox Code Playgroud)

这导致:

SyntaxError:扫描字符串文字时的EOL


这个答案建议使用:

my_str.replace('\\\\','\\')
Run Code Online (Sandbox Code Playgroud)

结果如下:

'\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z\\x'
Run Code Online (Sandbox Code Playgroud)

所以,没有变化.


这个答案表明:

b = bytes(my_str, encoding='utf-8')
b.decode('unicode-escape')
Run Code Online (Sandbox Code Playgroud)

但是这对于这样一个复杂的字符串不起作用:

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


使用解码(如此处所示)会导致:

my_str.decode('unicode_escape')
Run Code Online (Sandbox Code Playgroud)

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\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'
Run Code Online (Sandbox Code Playgroud)

python string replace python-3.x

5
推荐指数
1
解决办法
1682
查看次数

将\ x转义字符串转换为UTF-8

如何转换看起来像'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'Perl或Python可读的字符串?

python unicode perl

4
推荐指数
1
解决办法
7701
查看次数

用真实的\ n输出替换其中带有\ n的文本

我正在尝试从杜松路由器获取配置,但是我遇到以下问题:

设置后

stdin, stdout, stderr = client1.exec_command('show configuration interfaces %s' % SID)
 CONFIG = stdout.read()
 print CONFIG
Run Code Online (Sandbox Code Playgroud)

它带给我像这样的东西

'description El_otro_Puerto_de_la_Routing-instance;\nvlan-id 309;\nfamily inet {\n    mtu 1600;\n    address 10.100.10.10/24;\n}\n'
Run Code Online (Sandbox Code Playgroud)

问题是我想以这种格式接收该信息:

'description El_otro_Puerto_de_la_Routing-instance;
 nvlan-id 309;
 nfamily inet {
  mtu 1600;
  address 10.100.10.10/24;
 }
Run Code Online (Sandbox Code Playgroud)

因此,我希望\ n实际上是换行符,而不仅仅是显示“ \ n”字符串。

python blank-line juniper

4
推荐指数
2
解决办法
8214
查看次数

为什么 print 在 Python 中返回 \\,而不是转义字符 \

下面的代码打印表情符号,如下所示:

print('\U0001F602')
print('{}'.format('\U0001F602'))
Run Code Online (Sandbox Code Playgroud)

但是,如果我\像下面这样使用,它会打印\U0001F602

print('\{}'.format('U0001F602'))
Run Code Online (Sandbox Code Playgroud)

为什么print('\{}'.format())返回的是\\,而不是转义字符\

我一直在检查这个并在谷歌中搜索,但找不到正确的答案。

python string format encode

4
推荐指数
1
解决办法
842
查看次数

使用Python将"\ x"替换为文本中的"0x"

我有一个包含的文本,\x我想用这个替换0x.我使用了以下Python命令,但都失败了:

>>> text= '\x1bs\x1b\x01Z\xa2\x8d\xa2^\xb9*d\x08\x10&B\xb1z\xd4\xa91\xa3D
\xaf\xa1\x9a\x94\x8c\xd3\xb2r\x80\xc3\xb7)\xd8\x1bi\x80\x81\x02\x04\x08\x10@
\x81}0\xa8J\x95\x02E\x08\x10 @\x81\x02\x04\x08\x10 @\x81\x02\x04\x08\x10@
\x9f+\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x1b\x02^\xd3Q\xcc\xd70\x0eB\x88A\x1chB\x1bL\x81\xadC\x00
\x84!\rr(\x07\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x03
\x00?\x1c\x1b0'
>>>
>>>
>>> text.replace('\x',' 0x')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape
>>>
>>>
>>> text.replace(u'\x',' 0x')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape
>>>
>>>
>>> text.replace(r'\x',' 0x')
'\x1bs\x1b\x01Z¢\x8d¢^¹*d\x08\x10&B±zÔ©1£D¯¡\x9a\x94\x8cÓ²r\x80÷)Ø\x1bi\x80
\x81\x02\x04\x08\x10 @\x81}0¨J\x95\x02E\x08\x10 @\x81\x02\x04\x08\x10 @\x81
\x02\x04\x08\x10@\x9f+\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x02^ÓQÌ×0\x0eB\x88A\x1chB\x1bL\x81\xadC
\x00\x84!\rr(\x07ÿ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x03
\x00?\x1c\x1b0'
>>>
>>>
>>> text.replace('\\x',' 0x')
'\x1bs\x1b\x01Z¢\x8d¢^¹*d\x08\x10&B±zÔ©1£D¯¡\x9a\x94\x8cÓ²r\x80÷)Ø\x1bi\x80 …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

3
推荐指数
1
解决办法
2157
查看次数