Python has a lib csv
that can help you, I think it is native and does not need to install anything, only matter, with it becomes very simple to work with files csv
, let’s explain, to test create a file to check the functionality:
tt.csv (a silly and clueless lol test)
Nome, nota, media, aprovado
ederwander, 5, 3, nao
thomas, 10, 8, sim
maria, 9, 7, sim
luiz, 0, 4, nao
joao, 6, 5, nao
fernanda, 2, 6, sim
Now we can demonstrate how easy it is to grab the column you want, notice that the file has standard delimiters separated by ,
and that has 4 columns, the first column begins with 0
the second column shall be the 1
and so on ...
So let’s make a simple example just to take the last column of the number 3
:
import csv
with open('tt.csv') as csv_file:
ler_csv = csv.reader(csv_file, delimiter=',')
ler_csv.__next__()
for coluna in ler_csv:
#print( row[0] + ', ' + row[1] + ', ' + row[2] )
print( coluna[3])
The above example will print column 4:
C:\Python33>python.exe testcsv.py
nao
sim
sim
nao
nao
sim
see in the code the other example print commented, if you want to print the other columns, I think that’s it, that’s the basics ...