0

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.

Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
  • 5
    Possible duplicate of [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – Maged Saeed Mar 26 '19 at 06:39
  • Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on. – CryptoFool Mar 26 '19 at 06:41
  • 1
    Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables. – CryptoFool Mar 26 '19 at 06:49

1 Answers1

1

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
CryptoFool
  • 21,719
  • 5
  • 26
  • 44