Nou*_*ra 4 python unicode python-3.x
我正在使用阿拉伯语文本,我想删除阿拉伯语标点符号示例:
s="????? ??????? ?? ??? ??????? ! ?????? ???????? ? ,? ?? .???????"
Run Code Online (Sandbox Code Playgroud)
我希望输出" ? ? "也被删除,因为当我使用时:
import string
tr= str.maketrans("","", string.punctuation)
Run Code Online (Sandbox Code Playgroud)
输出是 '????? ??????? ?? ??? ??????? ?????? ???????? ? ?? ???????'
该string.punctuation常量仅包含在 ASCII 中定义的标点符号,它甚至不包括与拉丁文字一起使用的所有符号(例如,缺少像 «» 这样的“花式引号”)。
如果您不想自己创建所有标点字符的列表(我不会),您可以使用Unicode 字符属性来决定一个字符是否是标点符号。内置unicodedata模块可让您访问以下信息:
>>> import unicodedata as ud
>>> for c in 'abc: ??? ?':
... print((c, ud.category(c))
a Ll
b Ll
c Ll
: Po
Zs
? Lo
? Lo
? Lo
Zs
? Po
Run Code Online (Sandbox Code Playgroud)
所有类别都是两个字母的代码,例如“Ll”代表“字母,小写”或“Po”代表“标点符号,其他”。所有标点字符都有一个以“P”开头的类别。
您可以使用此信息过滤掉标点符号(例如,使用生成器表达式):
>>> s = "????? ??????? ?? ??? ??????? ! ?????? ???????? ? ,? ?? .???????"
>>> ''.join(c for c in s if not ud.category(c).startswith('P'))
'????? ??????? ?? ??? ??????? ?????? ??????? ?? ???????'
Run Code Online (Sandbox Code Playgroud)
小智 5
我正在研究类似的问题,并遇到了这篇文章。这是我用来解决它的。希望这可以帮助。
s="????? ??????? ?? ??? ??????? ! ?????? ???????? ? ,? ?? .???????"
new_s = s.translate(str.maketrans('', '', '???')) # add punctuation here
print(new_s)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2051 次 |
| 最近记录: |