简体   繁体   English

为什么我的程序不会从大型数据文件中打印列表?

[英]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我必须从大型数据文件中打印月份列表(月份被归类为 1=1 月、2=2 月、3=3 月等)但该程序仅打印 1 的列表,即使数据中有月份文件

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我希望在一个列表中输出所有月份数,但该列表只有 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.假设您拥有的 csv 文件中包含列名,最好使用 Pandas 尝试添加示例 csv,以便轻松提供解决方案。

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).但是如果我把 next(j) 放在那里就行不通了。

you didnt get expected output, because your are printing row[2] in for loop.您没有得到预期的输出,因为您正在 for 循环中打印行 [2]。 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.尝试附加到另一个列表中,您将根据需要将所有内容都放在一个列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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