如果在python中包含非英语单词,则如何删除整个字符串[重复]

问题描述 投票:0回答:1

我有一个数据框,其中的一列是'文本',如果该单元格由非英语单词组成,我正在尝试清理整个单元格。

我从单元格中删除了所有标点符号。我从单元中删除了所有非ASCI字符。我正在尝试导入其中一个英语词汇表,将单词转换为小写并检查我的单元格中的单词是否在该词典中。但是,由于处理堆叠,我没有得到任何输出。

places = []
with open('english-words/words.txt', 'r') as filehandle:
    for line in filehandle:
        currentPlace = line[:-1]
        currentPlace=currentPlace.lower()
        places.append(currentPlace)

def non_eng(texx):
    texx=texx.lower()
    s=[]
    s=texx.split()
    zz=''
    for i in s:
        if i in places:
            zz+=" "+i
    return zz
df['text']=df['text'].map(non_eng)

是否有一种更好的检查方法,而该单元格由英语单词而不是法语/意大利语等组成?

python pandas replace non-english
1个回答
0
投票

Detect strings with non English characters in Python

请参考上面给出的有关识别非英语字符串的链接。

def isEnglish(s):
    try:
        s.encode(encoding='utf-8').decode('ascii')
    except UnicodeDecodeError:
        return False
    else:
        return True

此函数将返回一个布尔值,说明字符串是否为英语。

© www.soinside.com 2019 - 2024. All rights reserved.