python - 从输出中删除 'None'

标签 python python-3.x

我正在尝试删除所有不属于法语的短语。我尝试使用 langdetect 库(不幸的是没有 pandas)

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:
    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)

关于python - 从输出中删除 'None',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51668931/

相关文章:

python - 如何将 "Tue Feb 25 2014 00:00:00 GMT+0530 (IST)"转换为 python 日期时间对象?

python - macOS - os.listdir 返回以 "."开头的双项?

python - 使用 python 将文本发送到带有逗号分隔符的列

python - __main__ 是否保证始终可导入?

python - 寻找列表中的下一个障碍

python - 使用 rpy2 导入并使用 R 'heavy' 包

集成在 GTK3/PyGObject 中的 Python 网络/tcp 包

python - 在python中乘以多项式

python - 如何在文本文件中查找字符串并每次用 Python 脚本中的用户输入替换?

python - 在 Python 中使用第三方库