我正在尝试使用以下格式导出一些数据
Sat Jan 3 18:15:05 2009 62e907b15cbf27d5425399ebf6f0fb50ebb88f18 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + 50.00000000
使用以下格式
进入csvFri Jan 9 03:23:48 2009,c1c6cb1b7d3c5a3f1dbd79ebd80d29bc6145e2fc,63522845d294ee9b0188ae5cac91bf389a0c3723f084ca1025e7d9cdfe481ce1,+, 50.00000000
不幸的是,这就是我得到的
(,',F,r,i, ,J,a,n, , ,9, ,0,3,:,2,3,:,4,8, ,2,0,0,9,',",", ,',c,1,c,6,c,b,1,b,7,d,3,c,5,a,3,f,1,d,b,d,7,9,e,b,d,8,0,d,2,9,b,c,6,1,4,5,e,2,f,c,',",", ,',6,3,5,2,2,8,4,5,d,2,9,4,e,e,9,b,0,1,8,8,a,e,5,c,a,c,9,1,b,f,3,8,9,a,0,c,3,7,2,3,f,0,8,4,c,a,1,0,2,5,e,7,d,9,c,d,f,e,4,8,1,c,e,1,',",", ,',+,',",", ,', , , , , , , , , , , , , , ,5,0,.,0,0,0,0,0,0,0,0,',)
这是我的代码
import csv
import struct
fieldwidths = (-4, 24, -4, 40,-4,64,-1,1,25) # negative widths represent ignored padding fields
fmtstring = ' '.join('{}{}'.format(abs(fw), 'x' if fw < 0 else 's')
for fw in fieldwidths)
fieldstruct = struct.Struct(fmtstring)
parse = fieldstruct.unpack_from
c = csv.writer(open("/home/ulrich/Desktop/disertation/sample_parsed_blch1.csv", "wb"))
fields = parse(" Sat Jan 3 18:15:05 2009 62e907b15cbf27d5425399ebf6f0fb50ebb88f18 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + 50.00000000")
c.writerow(format(fields))
答案 0 :(得分:1)
问题是您将fields
格式化为字符串。 writerow
将传递给它的集合的每个元素视为csv行中的一列。对于单个字符串,这意味着每个字符都是一个单独的列。假设fields
是解析该行的结果,只需直接使用它即可。
import csv
import struct
fieldwidths = (-4, 24, -4, 40,-4,64,-1,1,25) # negative widths represent ignored padding fields
fmtstring = ' '.join('{}{}'.format(abs(fw), 'x' if fw < 0 else 's')
for fw in fieldwidths)
fieldstruct = struct.Struct(fmtstring)
parse = fieldstruct.unpack_from
c = csv.writer(open("/home/ulrich/Desktop/disertation/sample_parsed_blch1.csv", "wb"))
fields = parse(" Sat Jan 3 18:15:05 2009 62e907b15cbf27d5425399ebf6f0fb50ebb88f18 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b + 50.00000000")
c.writerow(fields)