为了账号安全,请及时绑定邮箱和手机立即绑定

从输出中删除“无”

从输出中删除“无”

德玛西亚99 2021-05-14 14:01:23
我正在尝试删除所有不属于法语的短语。我尝试使用langdetect库(不幸的是没有熊猫)CSV文件messageJe suis fatiguéeThe book is on the tableIl fait chaud aujourd'hui!They are sicksLa vie est belle脚本:import csvfrom langdetect import detectwith 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))我的输出:NoneJe suis fatiguéeNoneIl fait chaud aujourd hui!NoneLa vie est belle我想:Je suis fatiguéeIl fait chaud aujourd hui!La vie est belle如何删除“无”?
查看完整描述

3 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

您只需在打印前添加一张支票即可:


result = detecteur_FR(message)

if result is not None:

    print(result)


查看完整回答
反对 回复 2021-05-18
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

您可以在打印消息之前进行比较吗?


convt_message = detecteur_FR(message)

if convt_message:

    print(convt_message)


查看完整回答
反对 回复 2021-05-18
?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

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


而是(全局)定义一次,仅在循环内调用它:


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)


查看完整回答
反对 回复 2021-05-18
  • 3 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号