Spaces:
Runtime error
Runtime error
import json | |
import csv | |
# Read JSON data | |
with open('/Users/simrankhanuja/Desktop/zeno-winoground/dataTagsSep4.json', 'r') as json_file: | |
data = json.load(json_file) | |
# Process and write to CSV | |
with open('output.csv', 'w', newline='') as csv_file: | |
writer = csv.writer(csv_file) | |
# Write header | |
header = ["id", "image", "caption", | |
"Old Tags: id", "Old Tags: tag", "Old Tags: secondary tag", | |
"Old Tags: num_main_preds", "Old Tags: collapsed tag", | |
"Our Tags: Attribute", "Our Tags: Relation", "Our Tags: Reasoning", "Why is WG hard"] | |
writer.writerow(header) | |
for entry in data: | |
for idx in range(2): # As there are two images/captions per entry | |
row = [] | |
row.append(f"{entry['id']}_{idx}") | |
row.append(entry[f'image_{idx}']) | |
row.append(entry[f'caption_{idx}']) | |
old_tags = entry["Old Tags"] | |
row.extend([old_tags['id'], | |
old_tags['tag'], | |
old_tags['secondary_tag'], | |
old_tags['num_main_preds'], | |
old_tags['collapsed_tag']]) | |
our_tags = entry["Our Tags"] | |
attribute = our_tags.get('Attribute', []) | |
relation = our_tags.get('Relation', []) | |
reasoning = our_tags.get('Reasoning', []) | |
row.extend([attribute, relation, reasoning]) | |
row.append(entry["Why is WG hard"]) | |
writer.writerow(row) | |