在Python中从CSV文件中读取行

时间:2012-11-17 06:32:37

标签: python csv

我有一个CSV文件,下面是一个示例:

Year:  Dec: Jan:
1      50   60
2      25   50
3      30   30
4      40   20
5      10   10

我知道如何读取文件并打印每一列(例如 - ['Year', '1', '2', '3', etc])。但我真正想要做的是读取行,就像这样['Year', 'Dec', 'Jan']然后['1', '50', '60'],依此类推。

然后我想将这些数字['1', '50', '60']存储到变量中,以便我可以在以后将这些数字加到变量中:

Year_1 = ['50', '60']。然后我可以sum(Year_1) = 110

我将如何在Python 3中实现这一目标?

10 个答案:

答案 0 :(得分:59)

使用csv module

import csv

with open("test.csv", "r") as f:
    reader = csv.reader(f, delimiter="\t")
    for i, line in enumerate(reader):
        print 'line[{}] = {}'.format(i, line)

输出:

line[0] = ['Year:', 'Dec:', 'Jan:']
line[1] = ['1', '50', '60']
line[2] = ['2', '25', '50']
line[3] = ['3', '30', '30']
line[4] = ['4', '40', '20']
line[5] = ['5', '10', '10']

答案 1 :(得分:16)

以列方式阅读更难?

无论如何,这会读取该行并将值存储在列表中:

for line in open("csvfile.csv"):
    csv_row = line.split() #returns a list ["1","50","60"]

现代解决方案:

# pip install pandas
import pandas as pd 
df = pd.read_table("csvfile.csv", sep=" ")

答案 2 :(得分:16)

你可以这样做:

with open("data1.txt") as f:
    lis = [line.split() for line in f]        # create a list of lists
    for i, x in enumerate(lis):              #print the list items 
        print "line{0} = {1}".format(i, x)

# output 
line0 = ['Year:', 'Dec:', 'Jan:']
line1 = ['1', '50', '60']
line2 = ['2', '25', '50']
line3 = ['3', '30', '30']
line4 = ['4', '40', '20']
line5 = ['5', '10', '10']

或:

with open("data1.txt") as f:
    for i, line in enumerate(f):             
        print "line {0} = {1}".format(i, line.split())

# output         
line 0 = ['Year:', 'Dec:', 'Jan:']
line 1 = ['1', '50', '60']
line 2 = ['2', '25', '50']
line 3 = ['3', '30', '30']
line 4 = ['4', '40', '20']
line 5 = ['5', '10', '10']

编辑:

with open('data1.txt') as f:
    print "{0}".format(f.readline().split())
    for x in f:
        x = x.split()
        print "{0} = {1}".format(x[0],sum(map(int, x[1:])))

# output          
['Year:', 'Dec:', 'Jan:']
1 = 110
2 = 75
3 = 60
4 = 60
5 = 20

答案 3 :(得分:5)

import csv

with open('filepath/filename.csv', "rt", encoding='ascii') as infile:
    read = csv.reader(infile)
    for row in read :
        print (row)

这将解决您的问题。不要忘记给出编码。

答案 4 :(得分:3)

render

有点晚但是......你需要创建和识别名为“years.csv”的csv文件:

年1月1日 1 50 60 2 25 50 3 30 30 4 40 20 5 10 10

答案 5 :(得分:3)

最简单的方法是这种方式:

from csv import reader

# open file in read mode
with open('file.csv', 'r') as read_obj:
    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Iterate over each row in the csv using reader object
    for row in csv_reader:
        # row variable is a list that represents a row in csv
        print(row)

output:
['Year:', 'Dec:', 'Jan:']
['1', '50', '60']
['2', '25', '50']
['3', '30', '30']
['4', '40', '20']
['5', '10', '10']

答案 6 :(得分:1)

csv模块按行处理csv文件。 如果要按列处理,pandas是一个很好的解决方案。

此外,有两种方法可以使用纯简单的Python代码获取所有(或特定的)列。

1。 csv.DictReader

with open('demo.csv') as file:
    data = {}
    for row in csv.DictReader(file):
        for key, value in row.items():
            if key not in data:
                data[key] = []
            data[key].append(value)

这很容易理解。

2。带zip的csv.reader

with open('demo.csv') as file:
    data = {values[0]: values[1:] for values in zip(*csv.reader(file))}

这不是很清楚,但是很有效。

zip(x, y, z)转置(x, y, z),而xyz是列表。 *csv.reader(file)用列名将(x, y, z)设为zip

演示结果

demo.csv的内容:

a,b,c
1,2,3
4,5,6
7,8,9

1的结果:

>>> print(data)
{'c': ['3', '6', '9'], 'b': ['2', '5', '8'], 'a': ['1', '4', '7']}

2的结果:

>>> print(data)
{'c': ('3', '6', '9'), 'b': ('2', '5', '8'), 'a': ('1', '4', '7')}

答案 7 :(得分:0)

一个人可以使用curl -v "api.com/test" --data "`curl -v -sG api.com/test -H "Authorization: Bearer ${token}" | jq '.[] | select(.query|test("latency")) | @json'`" 库来做到这一点。

示例:

pandas

答案 8 :(得分:0)

示例:

import pandas as pd

data = pd.read_csv('data.csv')

# read row line by line
for d in data.values:
  # read column by index
  print(d[2])

答案 9 :(得分:0)

我将解决方案留在这里。

import csv
import numpy as np

with open(name, newline='') as f:
    reader = csv.reader(f, delimiter=",")
    # skip header
    next(reader)
    # convert csv to list and then to np.array
    data  = np.array(list(reader))[:, 1:] # skip the first column

print(data.shape) # => (N, 2)

# sum each row
s = data.sum(axis=1)
print(s.shape) # => (N,)