I'm converting csv file to yaml file using python.how can i remove the quotes around the string and int in the yaml file and please also tell a way to remove ... between the lines and please also help with indentation
input in csv file is :-
{Field name,type,maxLength,Description}
{name,string,20,sdscbjxfgc xtcvhgx}
{DOB,Number,6,the date of birth}
my python code is:
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)
i'm expecting the output to be like :
type: object
properties:
name:
type: string
MaxLength: 20
description:|
name of the person
DOB:
type: Number
MaxLength: 6
description:|
the date of birth
but i'm getting this output:
type: object
properties:
name:
type: string
MaxLength: '20'
description:|
...
sdscbjxfgc xtcvhgx
...
DOB:
type: Number
MaxLength: '6'
description:|
...
the date of birth
...
As per basic python by default file opener will consider as string internally.
If you will replace 'MaxLength':row[2],
with 'MaxLength':int(row[2]),
then you will get expected output.
[Que] How to remove quotes around 'properties:'
Ans. 1st parameter of yaml.safedump can accept any datatype. And in your scenario (properties: with colon) internally its considering as dictionary. So you can pass hard-coded value properties as dictionary instead of list and value as None.
Check given link for more information Can I dump blank instead of null in yaml/pyyaml?
I have not worked on YAML so, I am not sure whether it is optimal solution or not. Please upvote if you find my answer useful.