我想将包含转义字符的字符串转换为正常形式,与Python的词法解析器相同:
>>> escaped_str = 'One \'example\''
>>> print(escaped_str)
One 'Example'
>>> normal_str = normalize_str(escaped_str)
>>> print(normal_str)
One 'Example'
当然,最无聊的方法是一个接一个地替换所有已知的转义字符:http://docs.python.org/reference/lexical_analysis.html字符串
如何在上面的代码中实现normalize_str()
?
>>> escaped_str = 'One \'example\ ">>> print escaped_str.encode('string_escape') "例子">>> print escaped_str.decode('string_escape')一个"例子"之前有几个类似的编解码器可用,如rot13和hex。
以上是Python 2。x,但是-既然你说(在下面的评论中)你正在使用Python 3。x -虽然解码Unicode字符串对象是迂回的,但仍然是可能的。编解码器也被重命名为"unicode_escape":
<>之前Python 3.3a0(默认值:b6aafb20e5f5, 2011年7月29日,05:34:11)[GCC 4.4.3输入"帮助"、"版权"、"信用"或"许可"获取更多信息。>>> escaped_str = "One \'example\'">>>导入编解码器>>>打印(codecs.getdecoder("unicode_escape")(escaped_str) [0])一个"例子"
我想问题真的是:
我有一个格式化的字符串,就好像它是Python源代码的一部分。我如何安全地解释它,以便将字符串中的
n
转换为换行符,两端都需要引号等?
Try ast.literal_eval
.
>>> import ast
>>> print ast.literal_eval(raw_input())
"hi, mom.n This is a "weird" string, isn't it?"
hi, mom.
This is a "weird" string, isn't it?
比较一下,用另一种方法:
>>> print repr(raw_input())
"hi, mom.n This is a "weird" string, isn't it?"
'"hi, mom.\n This is a \"weird\" string, isn't it?"'
SingleNegationElimination已经提到了这一点,但这里有一个例子:
在Python 3:
>>>escaped_str = 'One \'example\''
>>>print(escaped_str.encode('ascii', 'ignore').decode('unicode_escape'))
One 'example'
未配对的反斜杠只是表示的工件,而不是实际存储在内部。如果尝试手动执行此操作,可能会导致错误。
如果您唯一感兴趣的是删除前面没有奇数反斜杠的反斜杠,您可以尝试while循环:
escaped_str = 'One \'example\''
chars = []
i = 0
while i < len(escaped_str):
if i == '\':
chars.append(escaped_str[i+1])
i += 2
else:
chars.append(escaped_str[i])
i += 1
fixed_str = ''.join(chars)
print fixed_str
之后检查你的变量,你就会明白为什么你所做的没有意义。
…但从侧面来说,我几乎100%肯定"Python的词法解析器的方式相同",它没有使用解析器,可以这么说。解析器用于语法,它描述了将单词组合在一起的方式。
您可能正在考虑词法内容验证,这通常使用正则表达式指定。解析器是一种更具挑战性和更强大的工具,对于线性字符串操作来说,它不是您想要弄乱的东西。