Python 3.x 从阿拉伯语字符串中删除英语单词

Python 3.x 从阿拉伯语字符串中删除英语单词,python-3.x,Python 3.x,给定一个包含阿拉伯语和英语混合的字符串,我想从中删除任何英语字符或单词,只留下一个阿拉伯语句子。以下代码不起作用。如何修改它 import string text = 'انا أحاول أن أعرف من انت this is not' maintext = ''.join(ch for ch in text if ch not in set(string.punctuation)) text = filter(lambda x: x==' ' or x not in string.p

给定一个包含阿拉伯语和英语混合的字符串,我想从中删除任何英语字符或单词,只留下一个阿拉伯语句子。以下代码不起作用。如何修改它

import string

text = 'انا أحاول أن أعرف من انت this is not'
maintext = ''.join(ch for ch in text if ch not in set(string.punctuation))
text = filter(lambda x: x==' ' or x not in string.printable , maintext)
print(text)

谢谢

您可以在此处尝试使用
re.sub

# -*- coding: utf-8 -*-
import re

text = 'انا أحاول أن أعرف من انت this is not'
output = re.sub(r'\s*[A-Za-z]+\b', '' , text)
output = output.rstrip()
print(output)
这张照片是:

انا أحاول أن أعرف من انت
作为旁注,我们捕获了regex模式
\s*[a-Za-z]+
中可能的前导空格,因为我们不希望导致包围英语单词的两个阿拉伯语单词融合在一起。但是,这会在RHS上留下空白,因此我们调用
rstrip()
来删除它。

以下是我的版本:

import string
import re

text = 'انا أحاول أن أعرف من انت this is not'
maintext = re.sub(r'[a-zA-Z]', '', text)
print(maintext)

所有其他答案都建议使用正则表达式,但您可以不用正则表达式,只使用字符串模块中的ascii字母

导入字符串
文本=‘这不是’
text=”“.join([char表示文本中的字符,如果字符不在字符串中。ascii_字母]).strip()
打印(文本)
输出

انا أحاول أن أعرف من انت

正则表达式模式,比如所有拉丁字母的[a-zA-Z]非常感谢你。等待8分钟接受此答案