import json
data1 = {
'no' : 1,
'name' : 'Nobel',
}
json_str = json.dumps(data1)
data2 = json.loads(json_str)
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data1, f)
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f)
import csv
rows = [{'gid': '111', 'title': '222'}]
fieldnames = ['gid', 'title']
with open('example.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'gid': '000', 'title': '111'})
writer.writerows(rows)
# with Header
with open('example.csv', mode='r', encoding='utf-8') as f:
reader = csv.DictReader(f)
headers = reader.fieldnames
for row in reader:
print(row)
# no Header
with open('example.csv', mode='r', encoding='utf-8') as f:
my_fields = ['gid', 'title']
reader = csv.DictReader(f, fieldnames=my_fields)
for row in reader:
print(row)
Top comments (0)