Reputation: 119
I have the result of a shell command as a string. I want to parse the string so that the dictionary object alone is saved in a json file.
s = "output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
"base_score": 0.92,
"metric": "ACCURACY"
}]
I tried the below approach to parse after the "=" but I am not sure that is robust.
i = iter(s)
a = '-'.join(itertools.takewhile(lambda x: x != '=', i))
print(a)
with open('data.txt', 'w') as outfile:
json.dumps(data, outfile)
json.dumps(a, indent=4)
Upvotes: 0
Views: 149
Reputation: 379
This can be done with a regular expression.
import json
import re
s = '''output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[
{
"base_score": 0.92,
"metric": "ACCURACY"
}]'''
import json
import re
dict_txt = re.search('\[([^]]+)', s).group(1).strip()
data = json.loads(dict_txt)
print(data)
The regular expression is similar to that described here.
Upvotes: 1
Reputation: 6641
You could try something like this:
import json
s = '''output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[
{
"base_score": 0.92,
"metric": "ACCURACY"
}]'''
data = json.loads(s.split('=')[1])
print(data)
with open('result.json', 'w') as fp:
json.dump(data, fp)
That will split the string on the =
and then parse the second element.
Upvotes: 2