【发布时间】: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)`
【问题讨论】: