Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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 列表超出范围:我试图查看该文件,但找不到错误所在_Python_Csv_Dictionary_Tags_Treetagger - Fatal编程技术网

Python 列表超出范围:我试图查看该文件,但找不到错误所在

Python 列表超出范围:我试图查看该文件,但找不到错误所在,python,csv,dictionary,tags,treetagger,Python,Csv,Dictionary,Tags,Treetagger,我用我的文件尝试了这个脚本,它包含大约16列和5243行 第一列分别是键(仅整数1到5243),第二列是值,它们是句子(句子可以很长,直到段落) 当我尝试使用小文件时,它可以正常工作。但如果使用真实文件,则无法正常工作 # -*- coding: UTF-8 -*- import codecs import re import os import sys, argparse import subprocess import pprint import csv from itertools im

我用我的文件尝试了这个脚本,它包含大约16列和5243行

第一列分别是键(仅整数1到5243),第二列是值,它们是句子(句子可以很长,直到段落)

当我尝试使用小文件时,它可以正常工作。但如果使用真实文件,则无法正常工作

# -*- coding: UTF-8 -*-
import codecs 
import re
import os
import sys, argparse
import subprocess
import pprint
import csv
from itertools import islice
import pickle

try:
    import treetaggerwrapper
    from treetaggerwrapper import TreeTagger, make_tags
    print("import TreeTagger OK")
except:
    print("Import TreeTagger pas Ok")

from itertools import islice
from collections import defaultdict

#export le lexique de sentiments
pickle_in = open("dict_pickle", "rb")
dico_lexique = pickle.load(pickle_in)


# extraction colonne verbatim
d = {}
with open(sys.argv[1], 'r', encoding='cp1252',) as csv_file:
    csv_file.readline()
    for line in csv_file:
        token = line.split(';')
        d[token[0]] = token[1]
#print(d)

tagger = treetaggerwrapper.TreeTagger(TAGLANG='fr')
d_tag = {}
for key, val in d.items():
    newvalues = tagger.tag_text(val)
    d_tag[key] = newvalues

#print(d_tag)


d_lemma = defaultdict(list)
for k, v in d_tag.items():
    for elem in v:
        parts = elem.split('\t')
        d_lemma[k].append(parts[2])

#print(d_lemma) 
print('ok')


结果


import TreeTagger OK
Traceback (most recent call last):
  File "CSV_dico.py", line 50, in <module>
    d_lemma[k].append(parts[2])
IndexError: list index out of range


不是解决方案,而是查找错误的提示:

尝试更改此部分:

# extraction colonne verbatim
d = {}
with open(sys.argv[1], 'r', encoding='cp1252',) as csv_file:
    csv_file.readline()
    for line in csv_file:
        token = line.split(';')
        d[token[0]] = token[1]
为此:

# extraction colonne verbatim
d = {}
with open(sys.argv[1], 'r', encoding='cp1252',) as csv_file:
    csv_file.readline()
    for line in csv_file:
        token = line.split(';')
        try:
            d[token[0]] = token[1]
        except:
            print(line)

这会给您错误的令牌行,您可以检查它是否有错误

打印并检查变量“token”中的内容,显然是空的。。。这意味着有一行没有“;”@ncica I print token,它为每一行提供了一个文件示例,您能展示一下您的token看起来如何吗?谢谢,我使用了您的解决方案来查找错误,它比您的工作得更好
# extraction colonne verbatim
d = {}
with open(sys.argv[1], 'r', encoding='cp1252',) as csv_file:
    csv_file.readline()
    for line in csv_file:
        token = line.split(';')
        try:
            d[token[0]] = token[1]
        except:
            print(line)