从csv文件中读取每一列

时间:2021-09-26 11:09:36

I want to read each column of a csv file and do some modification before storing them into table.

我想读取csv文件的每一列,并在将它们存储到表中之前进行一些修改。

I have a csv files as :

我有一个csv文件:

"1";"testOne";"ValueOne"
"2";"testTwo";"ValueTwo"
"3";"testThree";"ValueThree"

Here I want to read the first value "1" and then store it somewhere in a varaible and do something with this value, and similary with the others. However currently I can read the whole file, but could not find the way to access individual columns in a row.

在这里,我想读取第一个值“1”,然后将其存储在变量中,并使用此值执行某些操作,并与其他值类似。但是目前我可以读取整个文件,但找不到连续访问各列的方法。

Thank you.

谢谢。

3 个解决方案

#1


22  

Python has a built-in csv module.

Python有一个内置的csv模块。

import csv
with open('some.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=';')
    for row in reader:
        print row[0]

#2


5  

The answer up top is flexible and pythonic, but if you wanted something a bit more compact and slick for retrieving entire columns at a time from delimited data (that fits comfortably into memory), you could also try this:

顶部的答案是灵活和pythonic,但如果你想要一些更紧凑和光滑的东西一次从分隔数据(舒适地适合内存)检索整个列,你也可以试试这个:

c_reader = csv.reader(open('test.csv', 'r'), delimiter=';')

# say you want the second column, only...
col_2 = list(zip(*c_reader))[1] # keeping in mind that python is 0-indexed

# or if you want to come back for more later on, you can just do...
columns = list(zip(*c_reader))

A bit more traditionally pythonic than the former but still functional feeling would be:

比传统的pythonic更具传统性,但仍具有功能性感觉:

# just using a good old list comprehension
col_2 = [x[1] for x in c_reader]

# you could also get all the rows simply in this way
rows = [x for x in c_reader]
row_2 = rows[1]

Now go forth and be one with the iterables! ;-)

现在出去与iterables成为一体! ;-)

#3


2  

You could use the csv python module:

你可以使用csv python模块:

class csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])

Create an object which operates like a regular reader but maps the information read into a dict whose keys are given by the optional fieldnames parameter. If the fieldnames parameter is omitted, the values in the first row of the csvfile will be used as the fieldnames. If the row read has more fields than the fieldnames sequence, the remaining data is added as a sequence keyed by the value of restkey. If the row read has fewer fields than the fieldnames sequence, the remaining keys take the value of the optional restval parameter. Any other optional or keyword arguments are passed to the underlying reader instance.

创建一个像常规阅读器一样操作的对象,但将读取的信息映射到一个dict,其键由可选的fieldnames参数给出。如果省略fieldnames参数,则csvfile的第一行中的值将用作字段名。如果读取的行包含的字段多于字段名序列,则将剩余数据添加为由restkey值键入的序列。如果读取的行的字段数少于字段名序列,则其余的键将采用可选的restval参数的值。任何其他可选或关键字参数都将传递给基础读取器实例。

#1


22  

Python has a built-in csv module.

Python有一个内置的csv模块。

import csv
with open('some.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=';')
    for row in reader:
        print row[0]

#2


5  

The answer up top is flexible and pythonic, but if you wanted something a bit more compact and slick for retrieving entire columns at a time from delimited data (that fits comfortably into memory), you could also try this:

顶部的答案是灵活和pythonic,但如果你想要一些更紧凑和光滑的东西一次从分隔数据(舒适地适合内存)检索整个列,你也可以试试这个:

c_reader = csv.reader(open('test.csv', 'r'), delimiter=';')

# say you want the second column, only...
col_2 = list(zip(*c_reader))[1] # keeping in mind that python is 0-indexed

# or if you want to come back for more later on, you can just do...
columns = list(zip(*c_reader))

A bit more traditionally pythonic than the former but still functional feeling would be:

比传统的pythonic更具传统性,但仍具有功能性感觉:

# just using a good old list comprehension
col_2 = [x[1] for x in c_reader]

# you could also get all the rows simply in this way
rows = [x for x in c_reader]
row_2 = rows[1]

Now go forth and be one with the iterables! ;-)

现在出去与iterables成为一体! ;-)

#3


2  

You could use the csv python module:

你可以使用csv python模块:

class csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])

Create an object which operates like a regular reader but maps the information read into a dict whose keys are given by the optional fieldnames parameter. If the fieldnames parameter is omitted, the values in the first row of the csvfile will be used as the fieldnames. If the row read has more fields than the fieldnames sequence, the remaining data is added as a sequence keyed by the value of restkey. If the row read has fewer fields than the fieldnames sequence, the remaining keys take the value of the optional restval parameter. Any other optional or keyword arguments are passed to the underlying reader instance.

创建一个像常规阅读器一样操作的对象,但将读取的信息映射到一个dict,其键由可选的fieldnames参数给出。如果省略fieldnames参数,则csvfile的第一行中的值将用作字段名。如果读取的行包含的字段多于字段名序列,则将剩余数据添加为由restkey值键入的序列。如果读取的行的字段数少于字段名序列,则其余的键将采用可选的restval参数的值。任何其他可选或关键字参数都将传递给基础读取器实例。