如何从python中删除阿拉伯字符串中的英文文本?

Ani*_*ish 5 python lambda nlp

我有一个带有英文文本和标点符号的阿拉伯字符串.我需要过滤阿拉伯语文本,我尝试使用sting删除标点符号和英语单词.但是,我丢失了阿拉伯语单词之间的间距.我哪里错了?

import string
exclude = set(string.punctuation)

main_text = "????? ????????: ?? ????? ????? ??????? ????? ?? ??????? ??????? ?? ????? http://alriyadh.com/1031499"
main_text = ''.join(ch for ch in main_text if ch not in exclude)
[output after this step="????? ???????? ?? ????? ????? ??????? ????? ?? ??????? ??????? ?? ????? httpalriyadhcom1031499]"
n = filter(lambda x: x not in string.printable, n)
print n
????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

我能够删除标点符号和英文文本,但我丢失了单词之间的空格.我怎样才能保留每一个字?

Bha*_*Rao 5

您可以使用保存字符串中的空格

n = filter(lambda x: True if x==' ' else x not in string.printable , main_text)
Run Code Online (Sandbox Code Playgroud)

要么

n = filter(lambda x: x==' ' or x not in string.printable , main_text)
Run Code Online (Sandbox Code Playgroud)

这将检查字符是否为空格,如果不是,则它将检查它是否可打印.