0

这是我当前的代码

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 列表。是否有可能做到这一点?如果是这样,你能提供我的代码吗?

4

5 回答 5

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']]
于 2013-06-22T06:24:28.900 回答
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
于 2013-06-23T18:27:38.603 回答
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()
于 2013-06-22T06:27:41.693 回答
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)
于 2013-06-22T12:41:58.870 回答
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]]

请注意,我更改了您命名的变量sumtotal防止与内置sum()函数发生冲突。

于 2013-06-22T12:34:23.087 回答