简体   繁体   中英

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. 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

There are a bunch of very similar issues on StackOverflow [ post 1 ], [ post 2 ].

For your reference, there is a much cleaner way of achieving this.
genfromtxt will suit your requirements pretty well. 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.

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 , .

my_data should now hold a numpy ndarray .

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