Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "data": [
- {
- "city": "A",
- "age": "21"
- },
- {
- "city": "B",
- "age": "45"
- }
- ]
- }
- import csv
- import datetime
- import mysql.connector as mariadb
- import simplejson
- import os
- mariadb_connection = mariadb.connect(user='user', password='passwd', database='db1')
- cursor = mariadb_connection.cursor()
- cursor.execute("SELECT name,age FROM persons ORDER BY name ASC")
- data = cursor.fetchall()
- names_as_dict = []
- for row in data:
- name_as_dict = {
- 'name' : row[0],
- 'age' : row[1]
- }
- names_as_dict.append(name_as_dict)
- with open('myfile.json', 'w') as f:
- simplejson.dump(names_as_dict, f)
- [
- {
- "city": "A",
- "age": "21"
- },
- {
- "city": "B",
- "age": "45"
- },
- ...
- ]
- {"data": names_as_dic}
Add Comment
Please, Sign In to add comment