【问题标题】:How do I unescape characters in python 3.6?如何在 python 3.6 中取消转义字符?
【发布时间】:2017-11-03 21:37:22
【问题描述】:

我对如何在 python 中转义字符有点困惑。我正在使用 BeautifulSoup 解析一些 HTML,当我检索文本内容时,它看起来像这样:

\u00a0\n\n\n\r\nState-of-the-art security and 100% uptime SLA.\u00a0\r\n\n\n\r\nOutstanding support

我希望它看起来像这样:

State-of-the-art security and 100% uptime SLA. Outstanding support

下面是我的代码:

    self.__page = requests.get(url)
    self.__soup = BeautifulSoup(self.__page.content, "lxml")
    self.__page_cleaned = self.__removeTags(self.__page.content) #remove script and style tags
    self.__tree = html.fromstring(self.__page_cleaned) #contains the page html in a tree structure
    page_data = {}
    page_data["content"] =  self.__tree.text_content()

如何删除那些编码的反斜杠字符?我到处看了看,没有什么对我有用。

【问题讨论】:

  • 你怎么知道内容是这样的?您是打印还是保存到文件中?
  • 我都做了,结果就是这样
  • 这就是你做print(page_data["content"])时得到的吗?你使用的是 Python 2 还是 Python 3?
  • 是的,我正在使用 python 3
  • 您能做到print(repr(page_data["content"])) 并将输出粘贴到您的问题中吗?

标签: python html python-3.x web-crawler


【解决方案1】:

您可以使用codecs 模块将这些转义序列转换为正确的文本。

import codecs

s = r'\u00a0\n\n\n\r\nState-of-the-art security and 100% uptime SLA.\u00a0\r\n\n\n\r\nOutstanding support'

# Convert the escape sequences
z = codecs.decode(s, 'unicode-escape')
print(z)
print('- ' * 20)

# Remove the extra whitespace
print(' '.join(z.split()))       

输出

    [several blank lines here]
 



State-of-the-art security and 100% uptime SLA. 



Outstanding support
- - - - - - - - - - - - - - - - - - - - 
State-of-the-art security and 100% uptime SLA. Outstanding support

codecs.decode(s, 'unicode-escape') 函数用途广泛。它可以处理简单的反斜杠转义,比如那些换行符和回车序列(\n\r),但它的主要优势是处理 Unicode 转义序列,比如\u00a0,它只是一个不间断的空格字符。但是,如果您的数据中包含其他 Unicode 转义符,例如用于外国字母字符或表情符号的转义符,它也会处理它们。


正如 Evpok 在评论中提到的那样,如果文本字符串包含实际的 Unicode 字符以及 Unicode \u\U 转义序列,这将不起作用

来自codecs docs

unicode_escape

适合作为 Unicode 文字内容的编码 ASCII 编码的 Python 源代码,但引号未转义。 从 Latin-1 源代码解码。当心 Python 源代码 实际上默认使用 UTF-8。

另请参阅codecs.decode 的文档。

【讨论】:

  • 由于unicode-escape 假定为 latin1 输入,因此包含 unicode 字符的字符串会失败
  • @Evpok 好点!我已经更新了我的答案。我必须承认,包含 Unicode 字符和 Unicode 转义序列混合的文本字符串会很奇怪,但我想我已经看到了各种奇怪的 Unicode。 ;) 至少 Python 3 在这方面比 Python 2 好很多。
  • 它实际上是在尝试取消转义 "l\'œil" 时咬我的,它不会转义 unicode,但仍然有转义。
  • @Evpok 如果这是 Python 脚本中的文字字符串,则不需要转义。 OTOH,如果这是您读入的数据,那么您实际上有r"l\'œil",相当于"l\\'œil",那么是的,unicode-escape 解码将无济于事。 stackoverflow.com/q/1885181/4014959 有一些建议,但其中一些答案仅适用于 Python 2。
  • 是的,谢谢,那里的 ast 解决方案实际上是我最终使用的 :-)
【解决方案2】:

你可以使用正则表达式:

import re

s = '\u00a0\n\n\n\r\nState-of-the-art security and 100% uptime SLA.\u00a0\r\n\n\n\r\nOutstanding support'
s = ' '.join(re.findall(r"[\w%\-.']+", s))

print(s) #output: State-of-the-art security and 100% uptime SLA. Outstanding support

re.findall("exp", s) 返回与模式“exp”匹配的 s 的所有子字符串的列表。在“[\w]+”的情况下,所有字母或数字的组合(没有像“\u00a0”这样的十六进制字符串):

['State', 'of', 'the', 'art', 'security', 'and', '100', 'uptime', 'SLA', 'Outstanding', 'support'] 

您可以通过将字符添加到表达式中来包含字符,如下所示:

re.findall(r"[\w%.-']+", s)    # added "%", "." and "-" ("-"needs to be escaped by "\")

' '.join(s) 返回由引号中的字符串分隔的所有元素的字符串(在本例中为空格)。

【讨论】:

  • 感谢工作的人,你能解释一下发生了什么吗?另外,我不想删除斜线,所以我不需要那部分。在我接受您的回答之前,我会看看可以提供哪些其他解决方案
  • s = '\u00a0\n\n\n\r\nState-of-the-art security and 100% uptime SLA.\u00a0\r\n\n\n\r\nOutstanding support' 不是问题中显示的数据。那只是普通的文字。您应该使用原始字符串将这些反斜杠序列作为文字字符串放入您的代码中。
猜你喜欢
  • 1970-01-01
  • 2013-06-09
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多