Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么line.split('\t')[1]不等于0?_Python_Python 3.x_List - Fatal编程技术网

Python 为什么line.split('\t')[1]不等于0?

Python 为什么line.split('\t')[1]不等于0?,python,python-3.x,list,Python,Python 3.x,List,我有许多包含两列的tsv文件。第一栏由句号组成,第二栏由这些句子的极性组成。分隔符是一个表格。我想提取极性为0的线 我编写了这个小代码,但不管怎样,它不起作用,返回0个句子 for d in directory: print(" directory: ", d) splits = ['dev1'] #,'test1','train1'] for s in splits: print(" sous-dir : ",

我有许多包含两列的tsv文件。第一栏由句号组成,第二栏由这些句子的极性组成。分隔符是一个表格。我想提取极性为0的线

我编写了这个小代码,但不管怎样,它不起作用,返回0个句子

    for d in directory:
        print(" directory: ", d)
        splits = ['dev1'] #,'test1','train1']

        for s in splits:

            print(" sous-dir : ", s)
            path = os.path.join(indir, d)
            with open(os.path.join(path, s+'.tsv'), 'r', encoding='utf-8') as f_in:
              next(f_in)
              for line in f_in:
                if line.split('\t')[1] == 0:
                  doc = nlp(line.split('\t')[0])

                  line_split = [sent.text for sent in doc.sents]

                  for elt in line_split:
                    sentences_list.append(elt)


    print("nombres total de phrases :", len(sentences_list))


如果line是字符串Je suis levant,为什么line.split'\t'[1]不等于0\t0\n

案卷

gnfjfklfklf  0
fokgmlmlrfm  1
eoklplrmrml  0
ekemlremeùe  0

我想保留以0结尾的行,在分割之后,您需要剥离字符串,以便移除IO放入其中的垃圾,例如换行符、其他选项卡等。因为Python有一个.strip函数

您还将在字符串和整数之间进行比较,因此为了使其不会因类型错误而失败,您必须更改代码以比较字符串,或者将结果从文件转换为int整数

条件可以重写为:

如果intline.split'\t'[1].strip==0:

或作为:


如果line.split'\t'[1].strip==0:

,因为它的长度为0\n 2。此外,按定义拆分会返回字符串,而0是int.Ok。谢谢,你知道我该如何改进才能提取以0结尾的部分吗;我尝试使用.endswith,但结果相同@decezeline.strip.endswith'0'…?使用strip函数删除换行符,然后将结果转换为int.intline.split'\t'[1]。strip。