简体   繁体   English

如何从python上的csv文件中读取列的行?

[英]how to read rows of a column from csv file on python?

activity=csv.reader(open('activity(delimited).csv')
data = np.array([activity])
url_data = data[:, 70]

I am trying to extract a column from the CSV file. 我试图从CSV文件中提取一列。 This column has a list of URLs that I would like to read. 此列包含我想要阅读的网址列表。 However every time I run these few lines, I get:- 但是每当我运行这几行时,我得到: -

IndexError: too many indices for array IndexError:数组的索引太多了

There are a bunch of very similar issues on StackOverflow [ post 1 ], [ post 2 ]. StackOverflow [ post 1 ],[ post 2 ]上有很多非常类似的问题。

For your reference, there is a much cleaner way of achieving this. 供您参考,有一种更清洁的方法来实现这一目标。
genfromtxt will suit your requirements pretty well. genfromtxt非常适合您的要求。 From the documentation, 从文档中,

Load data from a text file, with missing values handled as specified. 从文本文件加载数据,并按指定处理缺失值。

Each line past the first skip_header lines is split at the delimiter character, and characters following the comments character are discarded. 超过第一个skip_header行的每一行都以分隔符分割,并且放弃注释字符后面的字符。

You would need to do something like this. 你需要做这样的事情。

from numpy import genfromtxt
my_data = genfromtxt('activity(delimited)', delimiter=',')

Given that yours is a csv, the delimiter is a , . 鉴于你的是csv,分隔符是a , .

my_data should now hold a numpy ndarray . my_data现在应该拥有一个numpy ndarray

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

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