【问题标题】:How to write multiple rows from a list into a csv file如何将列表中的多行写入csv文件
【发布时间】:2020-12-08 05:45:47
【问题描述】:

我已经能够实施第 1 部分。但无法为以下内容执行第 2 部分:

  1. 已处理的库存报告: 一种。 FullInventory.csv - 按行列出的所有项目及其所有信息。 . 项目应按制造商的字母顺序排序。每行应包含项目 ID, 制造商名称、项目类型、价格、服务日期以及是否损坏的清单。 项目属性必须按此顺序出现。 湾。物品类型库存清单,即LaptopInventory.csv——每个物品都应该有一个文件 类型和项目类型需要在文件名中。文件的每一行都应包含 商品 ID、制造商名称、价格、服务日期和损坏清单。这几项 应按其项目 ID 排序。

     for i in range(len(csv_content)):
         file_name = csv_content[i][2]
         for j in range(i+1 , len(csv_content)):
             if 'laptop' == csv_content[j][2]:
                     with open(file_name + '.csv', 'w', newline='') as file:
                         writer = csv.writer(file)
                         writer.writerow(csv_content)
    

【问题讨论】:

标签: python python-3.x csv export-to-csv


【解决方案1】:

我想这就是你想要的

import csv

ls = [['Name'], ['name1'], ['name2'], ['name3']]
f = open('sample.csv', 'w+', newline ='')

with f:
    w = csv.writer(f)
    w.writerows(ls)

【讨论】:

  • 我正在尝试从具有匹配的不同列表名称的 csv 文件中访问 ls = [['Name'], ['name1'], ['name2'], ['name3']]列表中的特定元素.. 我不能使用 panda 或 lambda
猜你喜欢
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 2014-06-27
  • 2016-09-26
  • 1970-01-01
相关资源
最近更新 更多