简体   繁体   中英

why won't my program print a list from a large data file?

I have to print a list of months from a large data file (months are classified as 1=january, 2=february, 3=march, etc) but the program is only printing lists of 1s even though there are there months in the data file

I've printed out the months by themselves, not as a list, ad they print just fine. It only happens when I try to print them out as a list

import numpy as np
import csv
with open('FileName') as i:
    j = csv.reader(i, delimiter='\t')
    for row in j:
        next(j)
        print(row[2])

I expected an output with all of the number months in one list, but the list is only 1s

Its better to use pandas assuming that the csv file u have has column names in it Try adding sample csv so that it would be easy to give solution.

import pandas
colnames = ["name1","name2","month"]
data = pandas.read_csv("filename.csv",names=colnames)

to get the list of months just use this

month = data.month.tolist()

I dont think that you need

next(j)

This works:

j = iter([1,2,3])
for month in j:
  print(month)

Output:

1
2
3

But it does not work if I put next(j).

you didnt get expected output, because your are printing row[2] in for loop. eventually you will get evry item in list in a loop.

try to append into another list, you will endup with everything in one list, as you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM