cat csv file
1,10000e,0,a18b8200,3,4,1,1d,0,3,2,0,0,ffff,3d3e68143f8d65f8c3284d00c2e52800,ffff,3d79bab5406dfdebc41
2 1,100022,0,8c5de9a0,0,4,1,1d,0,3,2,0,0,ffff,10b30b00000000000010a3040001b380
3 1,10007,0,8ed525c0,3,4,1,1f,0,3,2,0,0,7777,00ffffff00ffffff00ffffff00ffffff,7777,00ffffff00ffffff00ff
4 1,1000bd,0,a18bb880,3,4,1,1f,0,3,2,0,0,ffff,3d21853a3f3b9bb2c2a98200421f9400,ffff,3d2188d53f3f08acc2a
5 1,1000e0,0,a18b9940,3,4,1,1e,0,3,2,0,0,ffff,3d298fa83fedc6c4c3801980c2d93600,ffff,3d2b3fa73ff191e3c38
6 1,10011,0,8ed32840,3,4,1,1a,0,3,2,0,0,7777,00ffffff00ffffff00ffffff00ffffff,7777,00ffffff00ffffff00ff
7 1,100137,0,8c406000,3,4,1,1f,0,3,2,0,0,7777,00ffffff00ffffff00ffffff00ffffff,7777,00ffffff00ffffff00f
....
在python代码中
def csv_slice(csvinfile, csvoutfile, ncol):
reader = csv.reader(open(csvinfile, 'rb'), delimiter=',')
writer = csv.writer(open(csvoutfile, 'wb'), delimiter=',')
for row in reader:
writer.writerow(row[ncol]) # result : a char
print row[ncol]
我是python中的新手。我怎样才能得到一个合适的值,这只是在列上? 上面的代码只能用逗号分隔字符,比如 想要的:10000e 但是,结果:每行1,0,0,0,0,e
先谢谢
答案 0 :(得分:1)
尝试
writer.writerow([row[ncol]]) # result : a char
writerow()
方法期望序列作为其参数。当你传递一个字符串时,它会假设字符串的每个字符都是该行的一个元素。建议的更改将所需的值转换为具有单个字符串元素的列表。