Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change string into Dict
#1
Hey,
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

Robin
Reply
#2
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
Reply
#3
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)
print(str)
use the json.dumps()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  change string in MS word Mr_Blue 8 3,218 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 2,490 May-24-2021, 08:48 AM
Last Post: bowlofred
Question dict value, how to change type from int to list? swissjoker 3 2,694 Dec-09-2020, 09:50 AM
Last Post: perfringo
  String index out of bounds ( Python : Dict ) kommu 2 2,351 Jun-25-2020, 08:52 PM
Last Post: menator01
  How can I change a string. Mike Ru 3 2,379 Jun-03-2020, 10:55 AM
Last Post: buran
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,152 May-17-2020, 05:56 PM
Last Post: shockwave
  Sort a dict in dict cherry_cherry 4 62,755 Apr-08-2020, 12:25 PM
Last Post: perfringo
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 2,645 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 2,488 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  [split] capitalize dict keys for display in string newbieAuggie2019 3 2,938 Oct-10-2019, 10:50 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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