我有一个文件bundle.crd。 itens就像这样
col1 val1 val2 val3 val4 col2 val1 val2 val3 val4 col4 val1 val2 val3 val4 col5 val1 val2 val3 val4 col6 val1 val2 val3 val4 col7 val1 val2 val3 val4
我需要以下列格式将其导入excel文件。
col1 col2 col3 col4 col5 col6 col7 ------------------------------------------- val1 val1 val1 val1 val1 val1 val1 val2 val2 val2 val2 val2 val2 val2 val3 val3 val3 val3 val3 val3 val3 val4 val4 val4 val4 val4 val4 val4
我尝试了这段代码,但它无效
lines = (line for line in stripped if line) grouped = itertools.izip(*[lines] * 3) with open('extracted.csv', 'w') as out_file: writer = csv.writer(out_file) #writer = csv.writer(out_file, dialect=csv.excel) writer.writerow(('title', 'intro', 'tagline')) writer.writerows(grouped)
请帮助
答案 0 :(得分:0)
您是否将线条拆分为单元格?
以下是一个示例,如何转置列表列表:
>>> values
[['col1', 'val1', 'val2', 'val3', 'val4'], ['col2', 'val1', 'val2', 'val3', 'val4'], ['col4', 'val1', 'val2', 'val3', 'val4'], ['col5', 'val1', 'val2', 'val3', 'val4'], ['col6', 'val1', 'val2', 'val3', 'val4'], ['col7', 'val1', 'val2', 'val3', 'val4']]
>>> zip(*values)
[('col1', 'col2', 'col4', 'col5', 'col6', 'col7'), ('val1', 'val1', 'val1', 'val1', 'val1', 'val1'), ('val2', 'val2', 'val2', 'val2', 'val2', 'val2'), ('val3', 'val3', 'val3', 'val3', 'val3', 'val3'), ('val4', 'val4', 'val4', 'val4', 'val4', 'val4')]