我想将数据列表放在一个csv文件中,这是我的数据
list_info = [('1552150125', '02141592cc00000001', '2', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000200', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '5', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000500', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '6', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000600', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '7', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000700', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '8', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000800', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '9', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000900', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '10', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000a00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '11', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000b00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '12', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000c00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '13', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000d00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '14', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000e00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '15', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000f00', '0', '0', '0', '23')]
这是我的代码:
print(list_info)
for row in list_info:
print (row)
for column in row:
out.write(column)
out.write('\n')
out.close()
但是它不起作用,它给了我在同一列中串联的每个列表,像这样:
155215012502141592cc000000012000000000001000000020000023
预期结果是:
time | ID |a|b|c|d|e|f|g|h|i|j|k|l|m|n |o|p|q|r|
1552150|02141592cc00000001|2|0|0|0|0|0|0|0|0|0|0|0|1|0000000200|0|0|0|23|
答案 0 :(得分:2)
您可以只使用csv
模块,
import csv
# your list_info here
header = 'time ID a b c d e f g h i j k l m n o p q r'.split()
with open('some_data.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter='|')
writer.writerow(header)
writer.writerows(list_info) # write all the rows at once as suggested by @roganjosh
答案 1 :(得分:0)
使用amount
软件包:
gateway.transaction.sale
答案 2 :(得分:-1)
具有基本Python工具的解决方案。
使用标准库的csv
模块也是一个可靠的解决方案。这仅使用str.format
。请注意,我建议在打开文件时始终使用编码。
out = open('test.csv', 'w', encoding='utf-8')
for row in list_info:
out.write('|'.join(row))
out.write('\n')
out.close()