Here's a Python program that converts a tag-based .csv file to a table-based .csv file:
import csv
from collections import defaultdict
def read_tag_based_csv(input_file):
with open(input_file, 'r') as file:
reader = csv.reader(file)
header = next(reader)
data = defaultdict(set)
unique_tags = set()
for row in reader:
obj, tags = row
tag_list = tags.split(';')
data[obj].update(tag_list)
unique_tags.update(tag_list)
return data, sorted(unique_tags)
def write_table_based_csv(output_file, data, unique_tags):
with open(output_file, 'w', newline='') as file:
writer = csv.writer(file)
header = ['object'] + unique_tags
writer.writerow(header)
for obj, tags in data.items():
row = [obj] + [tag in tags for tag in unique_tags]
writer.writerow(row)
def convert_tag_based_csv(input_file, output_file):
data, unique_tags = read_tag_based_csv(input_file)
write_table_based_csv(output_file, data, unique_tags)
if __name__ == "__main__":
input_file = 'input.csv'
output_file = 'output.csv'
convert_tag_based_csv(input_file, output_file)
To use this script, replace the input_file and output_file variables with the appropriate file paths for your input and output .csv files, respectively. The script reads the input file, collects the unique tags, and then writes the table-based .csv file to the output file.