-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_csv_to_json.py
40 lines (37 loc) · 1.08 KB
/
convert_csv_to_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import csv
import json
# https://docs.python.org/2/library/csv.html
file_name = 'singers.csv' # change to the csv file name that you are trying to upload
with open(file_name) as csvfile:
csvfile = csv.DictReader(csvfile)
app_name = 'myapp' # change this to your Django app name
model_name = 'person' # the name of you Django model
field_1 = 'first_name' # the name of first field
field_2 = 'last_name' # the name of second field
x = 0
output = []
for each in csvfile:
x += 1
row = {}
row = {'model': app_name+'.'+model_name, 'pk': x, 'fields': ({field_1: each[field_1], field_2: each[field_2]})}
output.append(row)
json.dump(output, open('converted_file.json','w'), indent=4, sort_keys=False)
#........ json data model example......
# [
# {
# "model": "myapp.person",
# "pk": 1,
# "fields": {
# "first_name": "John",
# "last_name": "Lennon"
# }
# },
# {
# "model": "myapp.person",
# "pk": 2,
# "fields": {
# "first_name": "Paul",
# "last_name": "McCartney"
# }
# }
# ]