删除unicode子弹与正则表达式(Python)- 未找到bu

2024-06-26 19:46:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在论坛里搜索了一下,试图删除所有似乎适用的解决方案。在

“弗里蒙特·全职”

你知道我怎样才能切出“Fremont”吗?在

我目前有这个删除项目符号,但我得到以下两个错误:

string.replace('/\d\.\s+|[a-z]\)\s+|[A-Z]\.\s+|[IVX]+\.\s+/g', "")

这对更改字符串没有任何影响,这说明regex查询无法识别小项目符号

^{pr2}$

这将产生以下错误:

UnicodeDecodeError:“ascii”编解码器无法解码位置20中的字节0xe2:序号不在范围内(128)

也尝试过这种方式

bullet = u"\u2022"
string =u"Fremont · Full Time"
string2 = string.replace(bullet, "A")
newStr = re.sub(regex, "", string)

没有效果

有什么想法吗?在


Tags: 项目字符串string错误符号解决方案论坛replace
2条回答

这样就可以了

import string
string_to_replace = "Fremont · Full Time"
regex = '\xc2\xb7'
string.replace(string_to_replace, regex, '')

我是怎么发现这个的

^{pr2}$

[编辑]
正如乔伊正确指出的,这取决于系统的编码。split函数应该有助于了解项目符号在系统中是如何编码的,以及您希望代码在哪些系统上运行。在

如果要将该字符串拆分为"Fremont"和{},只需使用:

>>> import re
>>> re.split(u"\s*·\s*", u"Fremont · Full Time")
[u'Fremont', u'Full Time']

相关问题 更多 >