0
Hello, I would like suggestions on how to read a csv file, I am maintaining a script, but it only returns the first line. I did some tests with some templates I found on the internet again only the first line, pq for each row will be done a check in sql for each contract.
data = csv.reader(open('caminho/contratos.csv', 'r'), delimiter=';')
for linha in data:
contrato = linha[0]
cidade = linha[1]
if contrato == 'CD_CONTRATO':
logging.info('IDENTIFICOU O CONTRATO: '+contrato)
continue
if cidade == 'ID_CIDADE':
logging.info('IDENTIFICOU A OPERADORA: '+cidade)
continue
Below the contents in csv file, first contract line, and the second the codcity of the base
1507499;806
2740829;054
612438310;884
612437764;884
15511642;013
thanks in advance for the help.
Show the 5 or 10 first lines of your csv.
– Sidon
1507499;806 2740829;054 612438310;884 612437764;884 15511642;013
– Lorena Jesus
Edit the question and show the first 5 lines of csv, in the comments do not to do this.
– Sidon
I edited the question, added 5 lines with the data in the csv file
– Lorena Jesus
I did a test with these lines and everything worked normal, your code to read the file and access the lines is ok, I just did not understand your explanation for the 5 lines q vc posted, it is only from a file?
– Sidon
is just a file, is that you asked to show 5 or 10 lines of my csv. That’s all.
– Lorena Jesus
Yes, run a test, create a new file
teste.csv
with these 5 lines, and open with the same command of the first line of your code in this question, then do:for linha in data: print(linha)
and see if you print the 5 lines, if yes, you must have problems with the original file– Sidon
It worked :), I made the change you suggested, thank you very much
– Lorena Jesus
@Lorenajesus if you can post the answer as solved the problem. Maybe it helps someone who is in the same doubt.
– Camilo Santos
Possible duplicate of How to read a CSV file in Python?
– LipESprY