我如何删除字符串周围的引号和转储到我的 yaml 文件中的数据中的 int
how can i remove the quotes in around the string and the int in the data dumped in my yaml file
我正在使用 python.how 将 csv 文件转换为 yaml 文件,我可以删除 yaml 文件中字符串和 int 两边的引号吗,还请告诉我如何删除...也有助于缩进
csv 文件中的输入是:-
{Field name,type,maxLength,Description}
{name,string,20,sdscbjxfgc xtcvhgx}
{DOB,Number,6,the date of birth}
我的 python 代码是:
def csvToYaml(csvFile, output):
stream = open(output, 'w',encoding='utf-8')
csvOpen = csv.reader(codecs.iterdecode(csvFile, 'utf-8'))
keys = next(csvOpen)
hardcoded=['type:object','properties:']
yaml.safe_dump(hardcoded,stream,default_flow_style=False,allow_unicode=True,sort_keys=False)
for row in csvOpen:
new = 'description:|'
list_1={row[0]:{
'type':row[1],
'MaxLength':row[2],
}}
yaml.safe_dump(list_1,stream,default_flow_style=False,sort_keys=False)
yaml.safe_dump(new,stream,default_flow_style=False,sort_keys=False)
yaml.safe_dump(row[3],stream,default_flow_style=False,sort_keys=False)
我希望输出如下:
type: object
properties:
name:
type: string
MaxLength: 20
description:|
name of the person
DOB:
type: Number
MaxLength: 6
description:|
the date of birth
但我得到了这个输出:
type: object
properties:
name:
type: string
MaxLength: '20'
description:|
...
sdscbjxfgc xtcvhgx
...
DOB:
type: Number
MaxLength: '6'
description:|
...
the date of birth
...
此完整代码(我必须添加所有导入和文件构建等):
与pyyaml:
import yaml
import csv
with open ("k.txt","w") as f:
f.write("""{Field name,type,maxLength,Description}
{name,string,20,sdscbjxfgc xtcvhgx}
{DOB,Number,6,the date of birth}""")
def csvToYaml(csvFile, output):
stream = open(output, 'w',encoding='utf-8')
csvOpen = csv.reader(csvFile)
keys = next(csvOpen)
hardcoded=['type:object','properties:']
yaml.safe_dump(hardcoded,stream,default_flow_style=False,allow_unicode=True,sort_keys=False)
for row in csvOpen:
list_1={row[0]:{
'type':row[1],
'MaxLength':row[2],
'description:|':row[3]
}}
yaml.safe_dump(list_1,stream,default_flow_style=False,sort_keys=False)
with open("k.txt") as f:
csvToYaml(f,"out.txt")
with open("out.txt") as f:
print(f.read())
打印:
- type:object
- 'properties:'
'{name':
type: string
MaxLength: '20'
description:|: sdscbjxfgc xtcvhgx}
'{DOB':
type: Number
MaxLength: '6'
description:|: the date of birth}
您需要删除对 yaml.safe_dump(new, ...)
的 "extraneous" 转储调用。它里面有多余的字符,您可能需要切换到其他一些 yaml 包或找到允许您最小化生成的 yaml 的参数。
from ruamel.yaml import YAML
import csv
with open ("k.txt","w") as f:
f.write("""Field name,type,maxLength,Description
name,string,20,sdscbjxfgc xtcvhgx
DOB,Number,6,the date of birth""")
yaml = YAML()
yaml.indent(mapping=4, sequence=6, offset=3)
def csvToYaml(csvFile, output):
stream = open(output, 'w',encoding='utf-8')
csvOpen = csv.reader(csvFile)
keys = next(csvOpen)
hardcoded=['type:object','properties:']
yaml.dump(hardcoded,stream)
for row in csvOpen:
list_1= { row[0]: {
'type':row[1],
'MaxLength':row[2],
'description:|':row[3]
}}
yaml.dump(list_1,stream)
with open("k.txt") as f:
csvToYaml(f,"out.txt")
with open("out.txt") as f:
print(f.read())
输出:
- type:object
- 'properties:'
name:
type: string
MaxLength: '20'
description:|: sdscbjxfgc xtcvhgx
DOB:
type: Number
MaxLength: '6'
description:|: the date of birth
根据基本 python 默认情况下,文件打开器将在内部将其视为字符串。
如果您将 'MaxLength':row[2],
替换为 'MaxLength':int(row[2]),
那么您将获得预期的输出。
[Que] 如何删除 'properties:'
周围的引号
答案。 yaml.safedump 的第一个参数可以接受任何数据类型。在您的场景中(属性:带冒号)在内部将其视为字典。因此,您可以将硬编码值 properties 作为字典传递,而不是将列表和值作为 None.
检查给出的 link 以获取更多信息
我没有在 YAML 上工作过,所以我不确定它是否是最佳解决方案。觉得我的回答有用请点个赞
只有 JSON 为我解决了类似的问题,抱歉 YAML。
我正在使用 python.how 将 csv 文件转换为 yaml 文件,我可以删除 yaml 文件中字符串和 int 两边的引号吗,还请告诉我如何删除...也有助于缩进
csv 文件中的输入是:-
{Field name,type,maxLength,Description}
{name,string,20,sdscbjxfgc xtcvhgx}
{DOB,Number,6,the date of birth}
我的 python 代码是:
def csvToYaml(csvFile, output):
stream = open(output, 'w',encoding='utf-8')
csvOpen = csv.reader(codecs.iterdecode(csvFile, 'utf-8'))
keys = next(csvOpen)
hardcoded=['type:object','properties:']
yaml.safe_dump(hardcoded,stream,default_flow_style=False,allow_unicode=True,sort_keys=False)
for row in csvOpen:
new = 'description:|'
list_1={row[0]:{
'type':row[1],
'MaxLength':row[2],
}}
yaml.safe_dump(list_1,stream,default_flow_style=False,sort_keys=False)
yaml.safe_dump(new,stream,default_flow_style=False,sort_keys=False)
yaml.safe_dump(row[3],stream,default_flow_style=False,sort_keys=False)
我希望输出如下:
type: object
properties:
name:
type: string
MaxLength: 20
description:|
name of the person
DOB:
type: Number
MaxLength: 6
description:|
the date of birth
但我得到了这个输出:
type: object
properties:
name:
type: string
MaxLength: '20'
description:|
...
sdscbjxfgc xtcvhgx
...
DOB:
type: Number
MaxLength: '6'
description:|
...
the date of birth
...
此完整代码(我必须添加所有导入和文件构建等):
与pyyaml:
import yaml
import csv
with open ("k.txt","w") as f:
f.write("""{Field name,type,maxLength,Description}
{name,string,20,sdscbjxfgc xtcvhgx}
{DOB,Number,6,the date of birth}""")
def csvToYaml(csvFile, output):
stream = open(output, 'w',encoding='utf-8')
csvOpen = csv.reader(csvFile)
keys = next(csvOpen)
hardcoded=['type:object','properties:']
yaml.safe_dump(hardcoded,stream,default_flow_style=False,allow_unicode=True,sort_keys=False)
for row in csvOpen:
list_1={row[0]:{
'type':row[1],
'MaxLength':row[2],
'description:|':row[3]
}}
yaml.safe_dump(list_1,stream,default_flow_style=False,sort_keys=False)
with open("k.txt") as f:
csvToYaml(f,"out.txt")
with open("out.txt") as f:
print(f.read())
打印:
- type:object
- 'properties:'
'{name':
type: string
MaxLength: '20'
description:|: sdscbjxfgc xtcvhgx}
'{DOB':
type: Number
MaxLength: '6'
description:|: the date of birth}
您需要删除对 yaml.safe_dump(new, ...)
的 "extraneous" 转储调用。它里面有多余的字符,您可能需要切换到其他一些 yaml 包或找到允许您最小化生成的 yaml 的参数。
from ruamel.yaml import YAML
import csv
with open ("k.txt","w") as f:
f.write("""Field name,type,maxLength,Description
name,string,20,sdscbjxfgc xtcvhgx
DOB,Number,6,the date of birth""")
yaml = YAML()
yaml.indent(mapping=4, sequence=6, offset=3)
def csvToYaml(csvFile, output):
stream = open(output, 'w',encoding='utf-8')
csvOpen = csv.reader(csvFile)
keys = next(csvOpen)
hardcoded=['type:object','properties:']
yaml.dump(hardcoded,stream)
for row in csvOpen:
list_1= { row[0]: {
'type':row[1],
'MaxLength':row[2],
'description:|':row[3]
}}
yaml.dump(list_1,stream)
with open("k.txt") as f:
csvToYaml(f,"out.txt")
with open("out.txt") as f:
print(f.read())
输出:
- type:object
- 'properties:'
name:
type: string
MaxLength: '20'
description:|: sdscbjxfgc xtcvhgx
DOB:
type: Number
MaxLength: '6'
description:|: the date of birth
根据基本 python 默认情况下,文件打开器将在内部将其视为字符串。 如果您将
'MaxLength':row[2],
替换为'MaxLength':int(row[2]),
那么您将获得预期的输出。[Que] 如何删除 'properties:'
周围的引号
答案。 yaml.safedump 的第一个参数可以接受任何数据类型。在您的场景中(属性:带冒号)在内部将其视为字典。因此,您可以将硬编码值 properties 作为字典传递,而不是将列表和值作为 None.
检查给出的 link 以获取更多信息
我没有在 YAML 上工作过,所以我不确定它是否是最佳解决方案。觉得我的回答有用请点个赞
只有 JSON 为我解决了类似的问题,抱歉 YAML。