Reputation: 5
I am working with a product data set for recommendations and I am trying to pre-process a string for keywords .
The string look like this and I am trying to get only the value portion of the string .
{"product_specification"=>[{"key"=>"Ideal For", "value"=>"Women"}, {"key"=>"Occasion", "value"=>"Casual"}, {"key"=>"Color", "value"=>"Red"}, {"key"=>"Outer Material", "value"=>"Patent Leather"}, {"key"=>"Heel Height", "value"=>"1 inch"}, {"key"=>"Number of Contents in Sales Package", "value"=>"Pack of 1"}, {"value"=>"One Pair Of Shoes"}]}
I want this string to be converted into a list of all values . For eg.
[Women, Casual, Red, Patent Leather, 1 inch, Pack of 1, One Pair Of Shoes]
How can I do this ?
Upvotes: 0
Views: 65
Reputation: 1215
This syntax is quite similar to a python dict, so after replacing =>
with :
, you can simply eval
or json.loads
to get the dictionary and then fetch its contents.
import json
str1 = '{"product_specification"=>[{"key"=>"Ideal For", "value"=>"Women"}, {"key"=>"Occasion", "value"=>"Casual"}, {"key"=>"Color", "value"=>"Red"}, {"key"=>"Outer Material", "value"=>"Patent Leather"}, {"key"=>"Heel Height", "value"=>"1 inch"}, {"key"=>"Number of Contents in Sales Package", "value"=>"Pack of 1"}, {"value"=>"One Pair Of Shoes"}]}'
str2 = str1.replace('=>', ':')
dikt = json.loads(str2)
items = dikt['product_specification']
result = [d['value'] for d in items]
print(result) # ['Women', 'Casual', 'Red', 'Patent Leather', '1 inch', 'Pack of 1', 'One Pair Of Shoes']
Upvotes: 2