【问题标题】:csv to yaml with multiple testingcsv 到 yaml 多次测试
【发布时间】:2023-02-23 18:33:22
【问题描述】:

我正在尝试编写一个可以将 csv 更改为 yaml 的 python 函数 现在我的 csv 看起来像

name,description,tests
product_id,ID of the product,not null|unique
product_name,Name of the product,not null

我希望输出是

 - name : product_id
   description: ID of the product
   tests:
   - not null
   - unique
 - name: product_name
   description: Name of the product
   tests: 
   - not null

现在我只有这个



for row_index, row in enumerate(datareader):
  if row_index == 0:
    # let's do this once here
    data_headings = list()
    for heading_index, heading in enumerate(row):
      fixed_heading = heading.lower().replace(" ", "_").replace("-", "")
      data_headings.append(fixed_heading)
      if fixed_heading == "type":
        type_index = heading_index
      elif fixed_heading == "childfields":
        child_fields_index = heading_index
  else:
    content = dict()
    is_array = False
    for cell_index, cell in enumerate(row):

     content[data_headings[cell_index]] = cell
     is_array = (cell_index == type_index) and (cell == "array")
    result.append(content)`

【问题讨论】:

    标签: python csv yaml


    【解决方案1】:

    我认为一个更pythonic的解决方案:

    import yaml
    import csv
    
    with open('sample_data.csv', 'r') as f:
        reader = csv.DictReader(f)
        data = []
        for row in reader:
            item = {}
            item['name'] = row['name']
            item['description'] = row['description']
            item['tests'] = row['tests'].split('|')
            data.append(item)
    
    with open('output.yaml', 'w') as f:
        yaml.dump(data, f)
    

    输入:

    name,description,tests
    product_id,ID of the product,not null|unique
    product_name,Name of the product,not null
    

    输出:

    - description: ID of the product
      name: product_id
      tests:
      - not null
      - unique
    - description: Name of the product
      name: product_name
      tests:
      - not null
    

    【讨论】:

      【解决方案2】:

      Python 的标准库有一个处理 CSV 文件的模块。这是DictReader班级 假定输入文件的第一行是列名(除非您提供 fieldnames 参数)。 使用它,您只需要为字段名称“测试”做一些特殊的事情。很遗憾 它(还)不能处理pathlib.Path()实例,所以你必须自己打开文件。

      您应该使用 ruamel.yaml 转储生成的数据结构,您必须将其安装在您的 virtualenv 中,例如与python -m pip install ruamel.yaml。它是一个 YAML 1.2(免责声明:我是该包的作者)。

      import csv
      from pathlib import Path
      import ruamel.yaml
      
      input = Path('input.csv')
      output = Path('output.yaml')
      
      data = []
      reader = csv.DictReader(input.open(newline=''))
      for row in reader:
          d = {}
          data.append(d)
          for field in reader.fieldnames:
              d[field] = row[field].split('|') if field == 'tests' else row[field]
      
      yaml = ruamel.yaml.YAML()
      yaml.dump(data, output)
      
      print(output.read_text())
      

      这使:

      - name: product_id
        description: ID of the product
        tests:
        - not null
        - unique
      - name: product_name
        description: Name of the product
        tests:
        - not null
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-22
        • 2012-03-31
        • 2017-07-18
        • 2010-10-08
        • 2015-10-30
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多