从输出中删除“None”

2024-09-29 23:18:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图删除所有不属于法语的短语。我试过使用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

脚本:

^{pr2}$

我的输出:

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

我怎样才能删除“无”?在


Tags: csvnonelailestjevielangdetect
3条回答

你能在打印邮件前做一下比较吗?在

convt_message = detecteur_FR(message)
if convt_message:
    print(convt_message)

在循环的每个迭代步骤中都要重新定义函数。在

相反,只需定义一次(全局)并且只在循环内调用它:

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)

只需在打印前添加支票:

result = detecteur_FR(message)
if result is not None:
    print(result)

相关问题 更多 >

    热门问题