Guest User

Untitled

a guest
Aug 22nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. {
  2. "data": [
  3. {
  4. "city": "A",
  5. "age": "21"
  6. },
  7. {
  8. "city": "B",
  9. "age": "45"
  10. }
  11. ]
  12. }
  13.  
  14. import csv
  15. import datetime
  16. import mysql.connector as mariadb
  17. import simplejson
  18. import os
  19.  
  20. mariadb_connection = mariadb.connect(user='user', password='passwd', database='db1')
  21. cursor = mariadb_connection.cursor()
  22. cursor.execute("SELECT name,age FROM persons ORDER BY name ASC")
  23.  
  24. data = cursor.fetchall()
  25. names_as_dict = []
  26.  
  27. for row in data:
  28. name_as_dict = {
  29. 'name' : row[0],
  30. 'age' : row[1]
  31. }
  32. names_as_dict.append(name_as_dict)
  33.  
  34. with open('myfile.json', 'w') as f:
  35. simplejson.dump(names_as_dict, f)
  36.  
  37. [
  38. {
  39. "city": "A",
  40. "age": "21"
  41. },
  42. {
  43. "city": "B",
  44. "age": "45"
  45. },
  46. ...
  47. ]
  48.  
  49. {"data": names_as_dic}
Add Comment
Please, Sign In to add comment