Skip to content

Commit 8d9591b

Browse files
committed
add example 15: process csv metadata into columns
1 parent b2a6ab8 commit 8d9591b

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
3+
# In this script, we use a CSV that has been created from eLabFTW
4+
# Then loop over all the entries, extract the metadata, and create another CSV
5+
# where each metadata extra field is a column
6+
import json
7+
import csv
8+
9+
# First we import a CSV file that has been created from eLabFTW export function
10+
# Important: use encoding parameter to decode the CSV properly (https://stackoverflow.com/a/49150749)
11+
with open('data/antibodies-export-from-elab.csv', encoding='utf-8-sig') as csvfile:
12+
# Use the DictReader function to get Dict objects back
13+
csvreader = csv.DictReader(csvfile)
14+
# We want all rows as a list of rows
15+
rows = []
16+
for row in csvreader:
17+
rows.append(row)
18+
19+
# This Set will be needed when exporting to csv
20+
fieldnames = set()
21+
for entry in rows:
22+
# Let's display for each row, the title and its corresponding metadata
23+
print(f"{entry.get('title')} => {entry.get('metadata')}")
24+
# Now let's process that metadata
25+
metadata = json.loads(entry.get('metadata'))
26+
# Extract fields from 'extra_fields' and add them to the item dictionary
27+
for key, value in metadata.get('extra_fields').items():
28+
entry[key] = value.get('value')
29+
# Remove the original JSON metadata
30+
del entry['metadata']
31+
fieldnames.update(entry.keys())
32+
33+
output = 'output.csv'
34+
print(f"[INFO] Writing output to {output}")
35+
with open(output, 'w') as f:
36+
writer = csv.DictWriter(f, fieldnames=fieldnames)
37+
writer.writeheader()
38+
writer.writerows(rows)

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ Get a list of events (booked slots) for the current team.
6161
# [14-fix-user-exp-permissions.py](./14-fix-user-exp-permissions.py)
6262

6363
Look for all experiments of a given user and adjust permissions of their experiments.
64+
65+
# [15-process-csv-metadata-into-csv-column.py](./15-process-csv-metadata-into-csv-column.py)
66+
67+
Take a CSV file created from eLabFTW export function, and for each row, grab the metadata, and create a new CSV where each Extra Field in the metadata is now a column.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,date,title,body,category,category_title,category_color,status,status_title,status_color,custom_id,elabid,rating,url,metadata,tags
2+
172,2024-03-28,Anti-Tubulin,,8,Antibody,C24F3D,3,In stock,c1e818,,20240328-cf7e261212d1d42bd58b36c213d0fb822043c2fd,0,https://elab.local:3148/database.php?mode=view&id=172,"{""extra_fields"": {""Host"": {""type"": ""select"", ""value"": ""Rabbit"", ""options"": [""Rabbit"", ""Mouse"", ""Goat"", ""Chicken""]}, ""Storage temperature"": {""type"": ""select"", ""value"": ""+4 °C"", ""options"": [""+4 °C"", ""-20 °C"", ""-80 °C""]}, ""Dilution to use for IF"": {""type"": ""text"", ""value"": ""1:100""}, ""Dilution to use for WB"": {""type"": ""text"", ""value"": ""1:1000""}, ""Number of aliquots left"": {""type"": ""number"", ""value"": ""12"", ""description"": ""Make sure to decrement this when using one!""}}}",
3+
174,2024-03-28,Anti-V5,,8,Antibody,C24F3D,3,In stock,c1e818,,20240328-0f967dd28c85db06f1b609d5ae468f1a04e68ca8,0,https://elab.local:3148/database.php?mode=view&id=174,"{""extra_fields"": {""Host"": {""type"": ""select"", ""value"": ""Rabbit"", ""options"": [""Rabbit"", ""Mouse"", ""Goat"", ""Chicken""]}, ""Storage temperature"": {""type"": ""select"", ""value"": ""+4 °C"", ""options"": [""+4 °C"", ""-20 °C"", ""-80 °C""]}, ""Dilution to use for IF"": {""type"": ""text"", ""value"": ""1:50""}, ""Dilution to use for WB"": {""type"": ""text"", ""value"": ""1:10000""}, ""Number of aliquots left"": {""type"": ""number"", ""value"": ""3"", ""description"": ""Make sure to decrement this when using one!""}}}",
4+
163,2024-03-28,Anti-Actin,,8,Antibody,C24F3D,3,In stock,c1e818,,20240328-7fa3f598c020da1593be7e07e3fdf78f8f4e5150,0,https://elab.local:3148/database.php?mode=view&id=163,"{""extra_fields"": {""Host"": {""type"": ""select"", ""value"": ""Rabbit"", ""options"": [""Rabbit"", ""Mouse"", ""Goat"", ""Chicken""]}, ""Storage temperature"": {""type"": ""select"", ""value"": ""+4 °C"", ""options"": [""+4 °C"", ""-20 °C"", ""-80 °C""]}, ""Dilution to use for IF"": {""type"": ""text"", ""value"": ""1:75""}, ""Dilution to use for WB"": {""type"": ""text"", ""value"": ""1:500""}, ""Number of aliquots left"": {""type"": ""number"", ""value"": ""10"", ""description"": ""Make sure to decrement this when using one!""}}}",

0 commit comments

Comments
 (0)