infile = "/path/to/input_file.csv"
outfile = "/path/to/output_file.csv"
with open(infile, 'r') as source:
num_fields = len(source.readline().split(','))
source.seek(0) # Go back to beginning
with open(outfile, 'w') as dest:
for n in range(num_fields):
for input_line in source:
dest.write(input_line.split(',')[n] + ',')
source.seek(0)
dest.write('\b\n') # remove trailing comma
如果CSV文件很大,并且您希望避免将数据加载到Python列表或字典中,可以执行以下操作:
否则,您只需加载所有内容:
^{pr2}$相关问题 更多 >
编程相关推荐