【问题标题】:how to remove whole string if it consists of non-english words in python [duplicate]如果它由python中的非英语单词组成,如何删除整个字符串[重复]
【发布时间】:2020-04-01 03:05:12
【问题描述】:

我有一个数据框,其中一列是“文本”,如果该单元格包含非英语单词,我正在尝试清理整个单元格。

我删除了单元格中的所有标点符号。 我从单元格中删除了所有非 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】:

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

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

【讨论】:

  • 如果我需要在同一个单元格中返回另一个输出,比如返回字符串,如果它只是英文字符?
猜你喜欢
  • 1970-01-01
  • 2014-05-19
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2013-08-11
  • 1970-01-01
  • 2019-08-03
相关资源
最近更新 更多