Error outside the list range

Asked

Viewed 45 times

0

I have a problem, I have 3 files that I need to open and get information from them, move to a list within the list (matrix?!) and compare information from 2 lists with another list to validate something. One of the files (that of technicians) holds registrations of technicians, another (regions) holds cities where IBGE surveys took place and the last is the file of the IBGE survey, i have to know if all the technicians that are in the IBGE research archive exist in the technicians archive and if the cities that are in the research archive exist in the regions archive that store all the cities that there was the research. I can pass the information to a "matrix", and compare the information however, I am comparing the lists but there is a part of my code that gives this error: Indexerror: list index out of range I could not see anything wrong so far in this, I have modified several times, put conditions (these conditions I put relates to the files, I basically made matched some things so that did not exceed the range of the files) but the error persists. This is my code:

pesquisa = []
arq_pesq = open('Pesquisa.txt', 'r')
matrizex=arq_pesq.readlines()
for line in matrizex[:]: 
    #Separa a string por ;
    Type = line.split(";") 
    a = Type[0] 
    b = Type[1]
    c = Type[2]
    d = Type[3]
    e = Type[4]
    f = Type[5]
    g = Type[6]
    h = Type[7]
    i = Type[8]
    j = Type[9]
    k = Type[10]
    l = Type[11]
    m = Type[12]
    n = Type[13]
    o = Type[14]
    p = Type[15]
    q = Type[16]
    r = Type[17]
    s = Type[18]
    t = Type[19]
    v= Type[20]
for i in range(1):
    linha = []
    for j in range(1):
        linha.append(Type)
    pesquisa.append(linha)


tecnicos=[]
arquivo_tec=open("tecnicosIBGE.txt" , 'r')
linhas=arquivo_tec.readlines()
for line in linhas[:]: 
    #Separa a string por ;
    Type = line.split(";") 
    a = Type[0] 
    b = Type[1]
    c = Type[2]
    d = Type[3]
    for i in range(1):
    linha = []
        for j in range(1):
            linha.append(Type)
        tecnicos.append(linha)

erros=[]   
n=200
c=142
contador=1
print(tecnicos[n][0][0])
print(pesquisa[c][0][0])
while True:
    n=200
    c=c-1
    contador=1
while(c!=0 or n!=0):
    if(c==0):
        break
    #O erro indica essa próxima linha
    elif tecnicos[n][0][0] in pesquisa[c][0][0]:
        print("Achou")
        print(tecnicos[n][0][0])
        print(pesquisa[c][0][0])
        n=200
        break
    else:
        if n==0:
            erros=pesquisa[c].append
            #del(pesquisa[c])
            break
        n=n-1
        if n==0:
            break
        contador=contador+1
        print(tecnicos[n][0][0])
        print(pesquisa[c][0][0])
        print("Não achou ",contador)

Files required for the program

1 answer

1

That suits you?

import csv

csv_tec = None
tec_matriculas = set()
with open('tecnicos.csv', mode = 'r') as f:
  csv_tec = csv.DictReader(f, delimiter = ';', quoting = csv.QUOTE_NONE)
  tec_matriculas = set([ x.get('Matrícula') for x in csv_tec])

csv_pes = None
pesq_tec = set()
with open('pesquisas.csv', mode = 'r') as f:
  csv_pes = csv.DictReader(f, delimiter = ';', quoting = csv.QUOTE_NONE)
  pesq_tec = set([ x.get("Técnico") for x in csv_pes ])

print(all(tecnico_pesquisa in tec_matriculas for tecnico_pesquisa in pesq_tec))

The idea here is to take advantage of the module csv Python and read as an Ordereddict (already with the keys). Those two set(...) serve only p/ remove duplicate entries. The all function will return True if for every element in pesq_tec (saved in the tecico_search variable) there is a corresponding in the set of enrollments extracted from the technicians file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.