forked from flingjie/CSVToOpenSeaJsonMetaData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
35 lines (30 loc) · 1.08 KB
/
convert.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
# python3.8
import csv
import os
from pathlib import Path
import json
if __name__ == "__main__":
filename = './data.csv'
directory = 'result'
keys = ['name', 'description', 'image', 'external_url']
Path(directory).mkdir(parents=True, exist_ok=True)
with open(filename, encoding='UTF-8-sig') as f:
data = csv.DictReader(f)
for i, line in enumerate(data):
if 'ID' not in line:
print(f'error: line {i+1} have no ID')
output = os.path.join(directory, line.get('ID'))
with open(output, 'w') as o:
print('==================================>')
print(line)
meta = {
'attributes': []
}
for k, v in line.items():
if k in keys:
meta.setdefault(k, v)
else:
meta['attributes'].append({"trait_type": k,
"value": v})
print(meta)
json.dump(meta, o)