【问题标题】:write data to csv | python将数据写入 csv | Python
【发布时间】:2021-07-22 12:11:39
【问题描述】:

需要建议,我们如何将输出数据写入 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.csv的预期内容?
  • 添加了 output.csv 的预期内容
  • 什么是 output_data?它是元组列表吗?当前不是有效的 Python 结构
  • @TrueEntertainer 我已经为您的问题发布了一个全面的答案

标签: python python-3.x csv


【解决方案1】:

您的代码存在一些问题,首先您需要将 output_data 变量重新构造为列表列表而不是元组列表,因为这不是写入 csv 的有效数据类型。然后你需要一个标题列表,并在打开语句中使用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

【讨论】:

  • 感谢@TERMINATOR,它真的很有帮助
猜你喜欢
  • 2016-03-27
  • 2015-01-21
  • 2018-06-25
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 2017-02-10
相关资源
最近更新 更多