需要关于如何将输出数据写入csv文件的建议。
output_data=
('6138', ['Test_storage_esx01', '07-22-2021', '01.30.00.0528'], 'policy1', 'Completed')
('6139', ['Test_storage_esx03', '07-22-2021', '02.30.00.0536'], 'policy1', 'Completed')
('6141', ['Test_storage_esx05', '07-22-2021', '04.00.00.0655'], 'policy1', 'Completed')
('6140', ['Test_storage_esx04', '07-22-2021', '03.00.00.0537'], 'policy1', 'Completed')
('6141', ['Test_storage_esx05', '07-22-2021', '04.00.00.0655'], 'policy1', 'Completed')
('6131', ['Test_storage_esx01', '07-22-2021', '22.00.00.0550'], 'policy1', 'Completed')
('6139', ['Test_storage_esx03', '07-22-2021', '02.30.00.0536'], 'policy1', 'Completed')
('6139', ['Test_storage_esx03', '07-22-2021', '02.30.00.0536'], 'policy1', 'Completed')
尝试了下面,但它现在工作
with open('output.csv', "wb") as csv_file:
writer = csv.writer(csv_file)
writer.writerows(output_data)
expected csv file output-
ID Storage date time policy status
6138 Test_storage_esx01 07-22-2021 01.30.00.0528 policy1 Completed
6139 Test_storage_esx03 07-22-2021 02.30.00.0536 policy1 Completed
6141 Test_storage_esx05 07-22-2021 04.00.00.0655 policy1 Completed
6140 Test_storage_esx04 07-22-2021 03.00.00.0537 policy1 Completed
6141 Test_storage_esx05 07-22-2021 04.00.00.0655 policy1 Completed
6131 Test_storage_esx01 07-22-2021 22.00.00.0550 policy1 Completed
6139 Test_storage_esx03 07-22-2021 02.30.00.0536 policy1 Completed
6139 Test_storage_esx03 07-22-2021 02.30.00.0536 policy1 Completed
您的代码有一些问题,首先您需要将output_data
变量重组为元组列表中的列表列表,因为这不是写入csv的有效数据类型。然后你需要一个标题列表,在open语句中使用w
模式而不是wb
,因为你不能在wb
模式中向文件写入字符串
代码:
import csv
header = ['ID','Storage','date','time','policy','status']
output_data = [
['6138', 'Test_storage_esx01', '07-22-2021', '01.30.00.0528', 'policy1', 'Completed'],
['6139', 'Test_storage_esx03', '07-22-2021', '02.30.00.0536', 'policy1', 'Completed'],
['6141', 'Test_storage_esx05', '07-22-2021', '04.00.00.0655', 'policy1', 'Completed'],
['6140', 'Test_storage_esx04', '07-22-2021', '03.00.00.0537', 'policy1', 'Completed'],
['6141', 'Test_storage_esx05', '07-22-2021', '04.00.00.0655', 'policy1', 'Completed'],
['6131', 'Test_storage_esx01', '07-22-2021', '22.00.00.0550', 'policy1', 'Completed'],
['6139', 'Test_storage_esx03', '07-22-2021', '02.30.00.0536', 'policy1', 'Completed'],
['6139', 'Test_storage_esx03', '07-22-2021', '02.30.00.0536', 'policy1', 'Completed']
]
with open('output.csv', "w") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(header)
writer.writerows(output_data)
输出:
ID Storage date time policy status
6138 Test_storage_esx01 07-22-2021 01.30.00.0528 policy1 Completed
6139 Test_storage_esx03 07-22-2021 02.30.00.0536 policy1 Completed
6141 Test_storage_esx05 07-22-2021 04.00.00.0655 policy1 Completed
6140 Test_storage_esx04 07-22-2021 03.00.00.0537 policy1 Completed
6141 Test_storage_esx05 07-22-2021 04.00.00.0655 policy1 Completed
6131 Test_storage_esx01 07-22-2021 22.00.00.0550 policy1 Completed
6139 Test_storage_esx03 07-22-2021 02.30.00.0536 policy1 Completed
6139 Test_storage_esx03 07-22-2021 02.30.00.0536 policy1 Completed