0

I have a large dictionary that is a created using a SQL select statement and .Dictcursor. It creates a dictionary of all the elements and the columns in the a format like this.

 {'EXAMPLE1': 'true',
  'EXAMPLE2': 'text',
  'EXAMPLE3': 'text',
  'EXAMPLE4': 'true',
  'EXAMPLE5': 'string'},

I want to replace the ' with " so it will work with SQL's syntax.

 {"EXAMPLE1": "true",
  "EXAMPLE2": "text",
  "EXAmPLE3": "text",
  "EXAMPLE4": "true",
  "EXAMPLE5": "string"},

I am generating the dictionary using this method

c = db.cursor(pymysql.cursors.DictCursor)
c.execute(sql)
data=c.fetchall()
c = db.cursor()
db.commit()
c.close()
pprint.pprint(data)

I am using Python3.8 and MySQL.

  • 1
    Use `json.dumps()` – Barmar May 12 '21 at 15:58
  • These dictionaries are completely identical. As in, Python stores them the same way in memory and literally can't tell them apart, so the idea of changing from one to the other makes no sense; you _can't_ convert from one to the other _because they're the same_. `"foo" == 'foo'` is true, and if you `print(repr("foo"))` and `print(repr('foo'))`, you get the same output. – Charles Duffy May 12 '21 at 15:58
  • 1
    @CharlesDuffy "work with SQL's syntax" implies (to me) that he wants to store it in a MySQL JSON column. Hence my suggestion. – Barmar May 12 '21 at 15:59
  • @Barmar, I agree, and it's a good suggestion, but I'm trying to get the question reframed into a format that doesn't have false implications. – Charles Duffy May 12 '21 at 15:59
  • 1
    @OwenCollins, ...so you can't transform the dictionary between the two formats because those formats both describe the same dictionary; all you can do is _convert it into a string_ that's serialized the way you need. – Charles Duffy May 12 '21 at 16:01

0 Answers0