Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 读取csv的特定列_Python 2.7_Csv - Fatal编程技术网

Python 2.7 读取csv的特定列

Python 2.7 读取csv的特定列,python-2.7,csv,Python 2.7,Csv,代码运行正常,但正在创建大括号中的值列表。我想修改代码,使其以正确的列和行格式以csv格式打印 预期产出: Ver Total 4 5 4 5 4 5 4 5 实际产量: (ver,total) (4,5) (4,5) (4,5) 下面是下面的代码 import csv f = open("a.csv", 'r') reader = csv.reader(f) data = [] for line in f: cells = line.spl

代码运行正常,但正在创建大括号中的值列表。我想修改代码,使其以正确的列和行格式以csv格式打印

预期产出:

Ver Total 

4     5

4     5

4     5

4     5
实际产量:

(ver,total) (4,5) (4,5) (4,5)
下面是下面的代码

import csv
f = open("a.csv", 'r')
reader = csv.reader(f)
data = []
for line in f:
    cells = line.split(",")
    data.append((cells[0], cells[3]))
print data
请尝试以下代码:

import csv

with open('a.csv') as csvfile:
    reader = csv.reader(csvfile)
    rowcnt = 0
    for row in reader:
        if rowcnt == 0:
            print row[0], row[1]
        else:
            print row[0], '  ', row[1]
        rowcnt = rowcnt + 1
提供以下输出:

Ver Stat
4    5
4    5
4    5

它给出了错误……回溯(最后一次调用):文件“D:\Python27\project\csvcol.py”,第17行,在for行中,在reader:ValueError:I/O操作上关闭的文件任何帮助都是非常感谢的。任何人都可以帮助这个问题,请有人帮助这个问题。。。M StuckIn缩进with块中的for块。