python - 如果它由python中的非英语单词组成,如何删除整个字符串
问题描述
我有一个数据框,其中一列是“文本”,如果该单元格包含非英语单词,我正在尝试清理整个单元格。
我删除了单元格中的所有标点符号。我从单元格中删除了所有非 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)
有没有更好的方法来检查单元格由英文单词而不是法语/意大利语等组成?
解决方案
请参考上面给出的关于识别非英文字符串的链接。
def isEnglish(s):
try:
s.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
此函数将返回一个布尔值,说明字符串是否为英文。
推荐阅读
- javascript - 使用 React-Native-Gifted-Chat 时如何隐藏 Android 键盘?
- entity-framework-core - 告诉 ef core migrations 来获取我的自定义迁移
- flutter - 交互式查看器缩放图像不起作用
- selenium-ide - 为部分文本匹配断言文本
- html - 将 div 移动到导航栏的右侧
- python - 使用第二列的百分比变化填充列中的空值,同时按多列分组
- url - 自定义 url scipt 或 wordpress 插件
- php - 站点地图优先级动态设置 PHP
- python-3.x - 使用 psycopg2 将 git 修订哈希 (SHA-1) 写入 PostgreSQL 数据库
- python - 如何在单个栏中绘制时间线?