Python正则表达式删除非unicode字符

标签 python

我正在尝试使用 python 正则表达式从字符串中删除一些看起来像非 unicode 的字符。 这是我的代码:

xxx='Juliana Gon\xe7alves Miguel'
t=re.sub('\w*','',xxx)
t

结果是这样的:

>>> xxx='Juliana Gon\xe7alves Miguel'
>>> t=re.sub('\w*','',xxx)
>>> t
' \xe7 '

这个\xe7 是我想要删除的。 大家有什么想法吗?

最佳答案

如果所需的输出是

'Juliana Gonalves Miguel'

那么下面的正则表达式应该可以解决问题。

re.sub('(?![ -~]).', '', xxx)

[ -~]:所有 ASCII 字符的简短且可读的版本

(?!):否定前瞻

关于Python正则表达式删除非unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38167273/

相关文章:

python - 从 pytest_generate_tests 中排除测试

python - "try"在 "def"之后没有通过

python - 在ubuntu python django服务中安装reportLab

php - 在写入时将文件推送到网络浏览器

python - PIL : Generating Vertical Gradient Image

python - 如何复制子列表中的公共(public)元素?

Python 导入在 travisCI 上失败但在本地失败

python - Python list.extend() 是 Order Presserving 吗?

python - 在 python 中,根据函数变量更改 MySQL 查询

python - 仅对齐并保存结构段