0
s= {'total_time': 39.420217514038086, 'fingerprint_time': 17.634710550308228, 'query_time': 12.381393909454346, 'align_time': 8.720932483673096, 'results': [{'song_id': 8, 'song_name':'Naio, Mr Music Man', 'input_total_hashes': 134302, 'fingerprinted_hashes_in_db': 134302, 'hashes_matched_in_input': 134302, 'input_confidence': 1.0, 'fingerprinted_confidence': 1.0, 'offset': 0, 'offset_seconds': 0.0, 'file_sha1':'0ED28744EF5238735DBC7F4F1F4FB96D61E07C6A'}, {'song_id': 9, 'song_name':'Naio, King of Kings', 'input_total_hashes': 134302, 'fingerprinted_hashes_in_db': 128063, 'hashes_matched_in_input': 38773, 'input_confidence': 0.29, 'fingerprinted_confidence': 0.3, 'offset': 1392, 'offset_seconds': 64.64435, 'file_sha1':'07B095A1D5CB590D9E308B05A4A7448E458443A0'}]}

data = [str(item) for item in s]

how can i convert all the valu ino string with apostrophe above is not working for me. all i want is to put all numbers in apostrophe.

need help

4
  • That is not a json, that is a dict Commented Oct 7, 2020 at 0:42
  • remove 'b' in front of all values should fixed json format. Commented Oct 7, 2020 at 1:01
  • What? No, you don't understand. JSON is a text-based serialization format. If you had JSON, you would be talking about some piece of text. What you have here is a dictionary, not JSON Commented Oct 7, 2020 at 1:06
  • thanks for pointing out. might as well need help to loop it through just need to get song_name values. Commented Oct 7, 2020 at 1:08

1 Answer 1

1

When iterating over a dictionary, by default you are iterating only over keys. You need to use the items() method to get both the key and value. Here for example:

data = {key: str(val) for key, val in s.items()}

If you strictly want the values as string, then:

data = [str(val) for val in s.values()]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks iam trying to get song_name iam using data = re.sub(": b",":", (str(dat))) to remve 'b' infront of the valus. data looks good but i can't loop it all to get song_name
@AlfredGeorge That seems like a different question altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.