从CSV Reader Python中分割出值

时间:2013-06-22 06:02:11

标签: python list csv split

这是我目前的代码

a_reader = None
a_reader     = open('data.csv', 'rU')
a_csv_reader = csv.reader(a_reader)

for row in a_csv_reader:
       print row
a_reader.close()

count = 0
sum   = 0.0
a_reader     = open('data.csv', 'rU')
a_csv_reader = csv.reader(a_reader)
a_csv_reader.next()

for row in a_csv_reader:
        if count != 0 and row[0] != '':
            sum = sum + float(row[0])
        count = count + 1

a_reader.close()
print 'Number of lines is:',count
print 'Sum is:',sum
return listStation

这会产生以下结果

['1', '476050', '7709929']    
['2', '473971', '7707713']    
['3', '465676', '7691097']    
['4', '515612', '7702192']    
['5', '516655', '7704405']    
['6', '519788', '7713255']    
['7', '538466', '7683341']    
Number of lines is: 8    
Sum is: 28.0

好了,我现在要做的是分割出ID,Easting和Northing的值,并将它们附加到列表中以创建一个2d列表。是否有可能做到这一点?如果是这样,你可以提供代码吗?

5 个答案:

答案 0 :(得分:2)

rows = []
for row in a_csv_reader:
       rows.append(row)

将在rows中产生:

[['1', '476050', '7709929']    
['2', '473971', '7707713']    
['3', '465676', '7691097']    
['4', '515612', '7702192']    
['5', '516655', '7704405']    
['6', '519788', '7713255']    
['7', '538466', '7683341']]

答案 1 :(得分:0)

试试这个:

import csv

def run():
    count = 0
    sum = 0.0
    listStation = []
    with open('data.csv', 'rU') as a_reader:
        a_csv_reader = csv.reader(a_reader)
        for row in a_csv_reader:
            if count != 0:
                if row[0] != '':
                    sum = sum + float(row[0])
                listStation.append(map(int, row))
            print 'row =', row
            count = count + 1
    print 'Number of lines is:',count
    print 'Sum is:', sum
    print listStation

if __name__ == '__main__':
    run()

答案 2 :(得分:0)

我没有你的data.csv文件来测试,但是这里是我如何重写代码并使它生成你想要的2D列表:

import csv

with open('test_data.csv', 'rU') as a_reader:
    a_csv_reader = csv.reader(a_reader)

    for row in a_csv_reader:
        print row

with open('test_data.csv', 'rU') as a_reader:
    a_csv_reader = csv.reader(a_reader)
    a_csv_reader.next()

    listStation = []
    count = 0
    total = 0.0
    for row in a_csv_reader:
        if count != 0 and row[0] != '':
            total += float(row[0])
        count += 1
        listStation.append(map(int, row))

print 'Number of lines is:', count
print 'Sum is:', total
print 'listStation:', listStation

输出:

['ID', 'Easting', 'Northing']
['1', '476050', '7709929']
['2', '473971', '7707713']
['3', '465676', '7691097']
['4', '515612', '7702192']
['5', '516655', '7704405']
['6', '519788', '7713255']
['7', '538466', '7683341']
Number of lines is: 7
Sum is: 27.0
listStation: [[1, 476050, 7709929], [2, 473971, 7707713], [3, 465676, 7691097], 
              [4, 515612, 7702192], [5, 516655, 7704405], [6, 519788, 7713255], 
              [7, 538466, 7683341]]

注意,我将您命名为sum的变量更改为total,以防止与内置sum()函数发生冲突。

答案 3 :(得分:0)

以下可能有效(取决于数据 - 在第一列中显示空白/否则列中存在无效数字等等):

from itertools import islice
import csv

with open('data.csv') as fin:
    csvin = islice(csv.reader(fin), 1, None) # skip header
    rows = [map(int, row) for row in csvin]

print 'Rows are:'
print rows
print 'Number of lines is:', len(stuff)
print 'Sum is:', sum(row[0] for row in stuff)

答案 4 :(得分:0)

我想我会用DictReader和默认字典编写你的代码:

import csv

data={}
with open('/tmp/sta.txt','r') as fin:
    reader=csv.DictReader(fin)
    for row in reader:
        for k,v in row.items():
            data.setdefault(k,[]).append(float(v))

print data
print 'Sum is:',sum(data['Station ID'])
print 'Number of lines is:',len(data['Station ID'])+1

打印:

{'Station ID': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 
 'Easting': [476050.0, 473971.0, 465676.0, 515612.0, 516655.0, 519788.0, 538466.0],
 'Northing': [7709929.0, 7707713.0, 7691097.0, 7702192.0, 7704405.0, 7713255.0, 7683341.0]}
Sum is: 28.0
Number of lines is: 8