-
Notifications
You must be signed in to change notification settings - Fork 0
/
pif2csv.py
43 lines (37 loc) · 992 Bytes
/
pif2csv.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
41
42
43
from pypif import pif
from pypif.obj import *
import csv
import json
# Open the json file that contains the pifs
json_file = open("pifs.json")
# Load json with pif
pifs = pif.load(json_file)
# Find all the CSV headers
headers = []
for p in pifs:
props = p.properties
names = []
for i in props:
names.append(i.name)
if len(names) > len(headers):
headers = names
# Possible attributes associated with a pif:
pif_common_objs = []
header = []
# CSV outfile
csv_out = "pifs.csv"
with open(csv_out, 'w', newline='') as outfile:
csvwriter = csv.writer(outfile)
csvwriter.writerow(headers)
# Loop through each pif
for p in pifs:
# Make a list of a row to write each time
row = []
# Add chemical_formula
row.append(p.chemical_formula)
#Add all the value of the properties
props = p.properties
for j in props:
row.append(j.scalars)
csvwriter.writerow(row)
outfile.close()