Python 拆下';无';从输出
我试图删除所有不属于法语的短语。我尝试使用langdetect库(不幸的是,没有熊猫) CSV文件Python 拆下';无';从输出,python,python-3.x,Python,Python 3.x,我试图删除所有不属于法语的短语。我尝试使用langdetect库(不幸的是,没有熊猫) CSV文件 message Je suis fatiguée The book is on the table Il fait chaud aujourd'hui! They are sicks La vie est belle 脚本: import csv from langdetect import detect with open('ddd.csv', 'r') as file: fichi
message
Je suis fatiguée
The book is on the table
Il fait chaud aujourd'hui!
They are sicks
La vie est belle
脚本:
import csv
from langdetect import detect
with open('ddd.csv', 'r') as file:
fichier = csv.reader(file)
for line in fichier:
if line[0] != '':
message = line[0]
def detecteur_FR(message):
#We need to turn the column into a list of lists.
message_list = [comments for comments in message.split('\n')]
for text in message_list:
if detect(text) == 'fr':
message_FR = text
return message_FR
print(detecteur_FR(message))
None
Je suis fatiguée
None
Il fait chaud aujourd hui!
None
La vie est belle
我的输出:
import csv
from langdetect import detect
with open('ddd.csv', 'r') as file:
fichier = csv.reader(file)
for line in fichier:
if line[0] != '':
message = line[0]
def detecteur_FR(message):
#We need to turn the column into a list of lists.
message_list = [comments for comments in message.split('\n')]
for text in message_list:
if detect(text) == 'fr':
message_FR = text
return message_FR
print(detecteur_FR(message))
None
Je suis fatiguée
None
Il fait chaud aujourd hui!
None
La vie est belle
我想:
Je suis fatiguée
Il fait chaud aujourd hui!
La vie est belle
如何删除“无”?您只需在打印前添加一张支票:
result = detecteur_FR(message)
if result is not None:
print(result)
在循环的每个迭代步骤中,您都在重新定义函数 相反,只定义一次(全局)并在循环内调用它:
import csv
from langdetect import detect
def detecteur_FR(message):
# We need to turn the column into a list of lists.
for text in message.split('\n'):
if detect(text) == 'fr':
return text
with open('ddd.csv', 'r') as file:
for line in csv.reader(file):
if line[0] != '':
result = detecteur_FR(line[0])
if result:
print(result)
我认为您得到的是“无”,因为您没有去掉每行末尾的“\n” 试试这个:
import csv
from langdetect import detect
def detecteur_FR(message):
#We need to turn the column into a list of lists.
message_list = [comments for comments in message.split('\n')]
for text in message_list:
if detect(text) == 'fr':
message_FR = text
print message_FR
with open('ddd.csv', 'r') as file:
fichier = csv.reader(file)
for line in fichier:
if line.strip() != '':
message = line[0]
detecteur_FR(message)
你能在打印信息之前进行比较吗
convt_message = detecteur_FR(message)
if convt_message:
print(convt_message)
我认为你不应该在那里定义你的函数,把它移到另一个作用域(可能是全局的)不,它们得到的是NOES,因为有些行不包含任何法语文本,因此永远不会显式返回任何内容。true,所以选项在函数内打印,或者验证输出是否为非。他们当前通过第一条法语线路上的
return
短路,但他们可能实际上需要所有输出