User Tools

Site Tools


convert_a_string_to_a_dictionary

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
convert_a_string_to_a_dictionary [2023/02/26 03:15] rajuconvert_a_string_to_a_dictionary [2023/02/26 03:18] – [json.loads] raju
Line 1: Line 1:
 ===== Convert a string to a dictionary ===== ===== Convert a string to a dictionary =====
  
 +==== ast.literaleval ====
 You can use ast.literaleval from the ast (Abstract Syntax Trees) module. You can use ast.literaleval from the ast (Abstract Syntax Trees) module.
  
Line 31: Line 32:
   * https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary - where I got the answer from   * https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary - where I got the answer from
  
 +==== json.loads ====
 Another approach is to use json.loads . But its decoder wants double quotes around strings. Another approach is to use json.loads . But its decoder wants double quotes around strings.
 <code> <code>
Line 45: Line 46:
 </code> </code>
  
-One workaround is to convert ' to "+One workaround is to replace single quotes with double quotes
 <code> <code>
 In [3]: In [3]:
convert_a_string_to_a_dictionary.txt · Last modified: 2023/02/26 03:28 by raju