zhu*_*yxn 204 python unicode parsing
我目前正在使用Beautiful Soup来解析HTML文件并调用get_text()
,但似乎我留下了很多代表空格的\ xa0 Unicode.有没有一种有效的方法可以在Python 2.7中删除所有这些,并将它们更改为空格?我想更普遍的问题是,有没有办法删除Unicode格式?
我尝试使用:line = line.replace(u'\xa0',' ')
,正如另一个线程所建议的,但是将\ xa0改为你的,所以现在我到处都是"u".):
编辑:问题似乎解决了str.replace(u'\xa0', ' ').encode('utf-8')
,但只是.encode('utf-8')
没有replace()
似乎导致它吐出甚至更奇怪的字符,例如\ xc2.有谁能解释一下?
sam*_*ize 234
\ xa0实际上是Latin1(ISO 8859-1)中的非破坏空间,也是chr(160).你应该用空格替换它.
string = string.replace(u'\xa0', u' ')
当.encode('utf-8')时,它会将unicode编码为utf-8,这意味着每个unicode可以用1到4个字节表示.对于这种情况,\ xa0由2个字节\ xc2\xa0表示.
阅读http://docs.python.org/howto/unicode.html.
小智 182
Python的unicodedata
库中有许多有用的东西.其中之一就是.normalize()
功能.
尝试:
new_str = unicodedata.normalize("NFKD", unicode_str)
Run Code Online (Sandbox Code Playgroud)
如果您没有得到您所获得的结果,请使用上面链接中列出的任何其他方法替换NFKD.
use*_*064 12
试试这个:
string.replace('\\xa0', ' ')
Run Code Online (Sandbox Code Playgroud)
小智 12
我遇到了同样的问题,用python从sqlite3数据库中提取一些数据.上面的答案对我不起作用(不确定原因),但是这样做了:line = line.decode('ascii', 'ignore')
但是,我的目标是删除\ xa0s,而不是用空格替换它们.
我从Ned Batchelder这个超级有用的unicode教程中得到了这个.
当谷歌搜索不可打印字符的问题时,我最终到了这里.我使用MySQL UTF-8
general_ci
并处理波兰语.对于有问题的字符串,我必须按如下方式进行:
text=text.replace('\xc2\xa0', ' ')
Run Code Online (Sandbox Code Playgroud)
这只是快速的解决方法,你可能应该尝试使用正确的编码设置.
试试这个代码
import re
re.sub(r'[^\x00-\x7F]+','','paste your string here').decode('utf-8','ignore').strip()
Run Code Online (Sandbox Code Playgroud)
小智 7
在 Beautiful Soup 中,您可以传递get_text()
strip 参数,该参数会去除文本开头和结尾的空白。\xa0
如果出现在字符串的开头或结尾,这将删除空格或任何其他空格。Beautiful Soup 替换了一个空字符串\xa0
,这解决了我的问题。
mytext = soup.get_text(strip=True)
Run Code Online (Sandbox Code Playgroud)
Python 将其识别为空格字符,因此您可以split
不使用 args 并通过普通空格连接:
line = ' '.join(line.split())
Run Code Online (Sandbox Code Playgroud)
在尝试了几种方法之后,总结一下,这就是我的方法。以下是避免/从解析的HTML字符串中删除\ xa0字符的两种方法。
假设我们的原始html如下:
raw_html = '<p>Dear Parent, </p><p><span style="font-size: 1rem;">This is a test message, </span><span style="font-size: 1rem;">kindly ignore it. </span></p><p><span style="font-size: 1rem;">Thanks</span></p>'
Run Code Online (Sandbox Code Playgroud)
因此,让我们尝试清除此HTML字符串:
from bs4 import BeautifulSoup
raw_html = '<p>Dear Parent, </p><p><span style="font-size: 1rem;">This is a test message, </span><span style="font-size: 1rem;">kindly ignore it. </span></p><p><span style="font-size: 1rem;">Thanks</span></p>'
text_string = BeautifulSoup(raw_html, "lxml").text
print text_string
#u'Dear Parent,\xa0This is a test message,\xa0kindly ignore it.\xa0Thanks'
Run Code Online (Sandbox Code Playgroud)
上面的代码在字符串中生成这些字符\ xa0。要正确删除它们,我们可以使用两种方法。
方法1(推荐): 第一个是BeautifulSoup的get_text方法,带参数为True, 因此我们的代码变为:
clean_text = BeautifulSoup(raw_html, "lxml").get_text(strip=True)
print clean_text
# Dear Parent,This is a test message,kindly ignore it.Thanks
Run Code Online (Sandbox Code Playgroud)
方法2: 另一个选择是使用python的库unicodedata
import unicodedata
text_string = BeautifulSoup(raw_html, "lxml").text
clean_text = unicodedata.normalize("NFKD",text_string)
print clean_text
# u'Dear Parent,This is a test message,kindly ignore it.Thanks'
Run Code Online (Sandbox Code Playgroud)
我还在此博客上详细介绍了这些方法,您可能想参考这些方法。
It's the equivalent of a space character, so strip it
print(string.strip()) # no more xa0
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
208126 次 |
最近记录: |