-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_library_fixture.py
47 lines (38 loc) · 1.38 KB
/
make_library_fixture.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
44
45
46
47
#!/usr/bin/python
import json
import numpy as np
import pandas as pd
filepath = "~/Downloads/lasfera.xlsx"
sheet_name = "Universal"
xls = pd.ExcelFile(filepath)
if sheet_name:
df = pd.read_excel(xls, sheet_name, header=1)
df = df.replace({np.nan: None})
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
dfs = {sheet_name: df}
else:
dfs = pd.read_excel(xls, sheet_name=None, header=1)
for sheet_name, df in dfs.items():
df = df.replace({np.nan: None})
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
dfs[sheet_name] = df
data = []
existing_libraries = set()
for sheet_name, df in dfs.items():
for index, row in df.iterrows():
library_city = row.get("city")
library_name = row.get("library")
# Create a unique identifier for each library
library_id = f"{library_name}-{library_city}"
# Only add the library if it's not already in the set
if library_id not in existing_libraries:
existing_libraries.add(library_id)
data.append(
{
"model": "manuscript.Library",
"pk": index + 1,
"fields": {"library": library_name, "city": library_city},
}
)
with open("manuscript/fixtures/libraries.json", "w", encoding="utf-8") as f:
json.dump(data, f)