Any suggestion on how to convert data as follows into csv file in python:
[{
'name': 'a',
'url': 'url1'
},
{
'name': 'b',
'url': 'url2'
},
{
'name': 'c',
'url': 'url3'
}]
thank you.
Any suggestion on how to convert data as follows into csv file in python:
[{
'name': 'a',
'url': 'url1'
},
{
'name': 'b',
'url': 'url2'
},
{
'name': 'c',
'url': 'url3'
}]
thank you.
It isn't as simple as what I said in my first comment. When I said that, I didn't realize that your input string isn't in any standard format that's easy to read with a JSON library or by interpreting as Python code.
Here's a very restrictive answer, that will only work on input strings that are of that very specific form:
data = "[{name:a,url:url1},{name:b,url:url2},{name:c,url:url3}]"
entries = re.findall("{([^:]+):([^,]+),([^:]+):([^}]+)", data)
with open("/tmp/output.csv", "w") as f:
f.write("Name,Url\n")
for entry in entries:
f.write(entry[1] + ',' + entry[3] + '\n')
Resulting file contents:
Name,Url
a,url1
b,url2
c,url3