Python dictionary converting to json or yaml
<p>I have parsed a string and converted into a dictionary. however I would like to be able to get more information from my dictionary and I was thinking creating a son file or yaml file would be more useful.
please feel free to comment on another way of solving this problem.</p>

<pre><code>import re
regex = '(\w+) \[(.*?)\] "(.*?)" (\d+) "(.*?)" "(.*?)"'
sample2= 'this [condo] "number is" 28 "owned by" "james"'
patern = '(?P&lt;host&gt;\S+)\s(?P&lt;tipe&gt;\[\w+\])\s(?P&lt;txt&gt;"(.*?))\s(?P&lt;number&gt;\d+)\s(?P&lt;txt1&gt;"\w+\s\w+")\s(?P&lt;owner&gt;"\w+)'
m =re.match(patern, sample2)
res = m.groupdict()
print res
</code></pre>

<p>and I would get:</p>

<pre><code>{'tipe': '[condo]', 'number': '28', 'host': 'this', 'owner': '"james', 'txt': '"number is"', 'txt1': '"owned by"'}
</code></pre>

<p>I would like to be able to sort by owner name and write out the type of house and the house number. for example:</p>

<p>James:{ type:condo, number: 28}</p>

<p>or any other suggestions?</p>

<p>The question of json vs yaml boils down to what are you doing with the data you are saving? If you are reusing the data in JS or in another python script then you could use JSON. If you were using it in something that uses YAML then use YAML. But in terms of your question, unless there is a particular end game or use case for your data, it doesn't matter the format, you could even just do a typical TSV/CSV and it'd be the same.</p>

<p>To answer your question about how to make a dict in to a json.</p>

<p>From your example once you have the <code>res</code> dictionary made.</p>

<pre><code>import json
with open('someFile.json', 'wb') as outfile:
     json.dump(res, outfile)
</code></pre>

<p>Your output file <code>someFile.json</code> will be your dictionary <code>res</code></p>

python json dictionary