从CSV阅读器分割值Python [英] Splitting values out of a CSV Reader Python

查看:131
本文介绍了从CSV阅读器分割值Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前的程式码

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

a_csv_reader中的行:
打印行
a_reader.close()
$ b b count = 0
sum = 0.0
a_reader = open('data.csv','rU')
a_csv_reader = csv.reader(a_reader)
a_csv_reader.next

a_csv_reader中的行:
if count!= 0 and row [0]!='':
sum = sum + float(row [0])
count = count + 1

a_reader.close()
print'行数为:',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']
行数为:8
总和为:28.0

现在我想做的是分离ID,Easting和Northing的值,并将它们添加到列表中以创建一个2d列表。有可能做到这一点吗?

$

  rows = [] $ b $ 

b for a_csv_reader:
rows.append(row)

c $ c> rows :

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


Here is my current code

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

This produces the results below

['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

Ok now what I want to do is to split out the value of the ID, Easting and Northing and append them to a list to create one 2d list. Is it possible to do this? If so can you provide me the code?

解决方案

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

Will yield in rows:

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

这篇关于从CSV阅读器分割值Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆