Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change string into Dict
I'm getting data from the web which is basically a dictionary. The problem is that one key refers to another dictionary which is unfortunately a string:

dict = {'somethin':'else','data':'{the other dict}',.......} 
for obvious reasons i want to change it to change it to:

{'somethin':'else','data':{the other dict},.......}
in words that '{the other dict}' goes to {the other dict}

Has someone an idea to do that quick and simple?

thanks in advance

A lot of data obtained over the web is obtained in JSON (JavaScript object notation) format, which include nesting of dictionaries in the manner you described. Not sure from your fragment if this is the case here, but it if is, you will be pleased to know there's a library just for this. A quick google of python JSON will lead you to good solution guides.
I am trying to help you, really, even if it doesn't always seem that way
Just to demonstrate what you can do using Python json module.

sampleObj = {
  "car1": { "name":"Ford", "price":35000},
  "car2": { "name":"BMW", "price":65000}

str = json.dumps(sampleObj, indent=4)
use the json.dumps()

Possibly Related Threads…
Thread Author Replies Views Last Post
  change string in MS word Mr_Blue 8 814 Sep-19-2021, 02:13 PM
Last Post: snippsat
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 960 May-24-2021, 08:48 AM
Last Post: bowlofred
Question dict value, how to change type from int to list? swissjoker 3 1,215 Dec-09-2020, 09:50 AM
Last Post: perfringo
  String index out of bounds ( Python : Dict ) kommu 2 1,092 Jun-25-2020, 08:52 PM
Last Post: menator01
  How can I change a string. Mike Ru 3 1,254 Jun-03-2020, 10:55 AM
Last Post: buran
  TypeError: __repr__ returned non-string (type dict) shockwave 0 1,559 May-17-2020, 05:56 PM
Last Post: shockwave
  Sort a dict in dict cherry_cherry 4 1,581 Apr-08-2020, 12:25 PM
Last Post: perfringo
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 1,287 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 1,202 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  [split] capitalize dict keys for display in string newbieAuggie2019 3 1,579 Oct-10-2019, 10:50 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020