Reputation: 37
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.
import re
regex = '(\w+) \[(.*?)\] "(.*?)" (\d+) "(.*?)" "(.*?)"'
sample2= 'this [condo] "number is" 28 "owned by" "james"'
patern = '(?P<host>\S+)\s(?P<tipe>\[\w+\])\s(?P<txt>"(.*?))\s(?P<number>\d+)\s(?P<txt1>"\w+\s\w+")\s(?P<owner>"\w+)'
m =re.match(patern, sample2)
res = m.groupdict()
print res
and I would get:
{'tipe': '[condo]', 'number': '28', 'host': 'this', 'owner': '"james', 'txt': '"number is"', 'txt1': '"owned by"'}
I would like to be able to sort by owner name and write out the type of house and the house number. for example:
James:{ type:condo, number: 28}
or any other suggestions?
Upvotes: 0
Views: 1347
Reputation: 4831
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.
To answer your question about how to make a dict in to a json.
From your example once you have the res
dictionary made.
import json
with open('someFile.json', 'wb') as outfile:
json.dump(res, outfile)
Your output file someFile.json
will be your dictionary res
Upvotes: 1