scripts/merge-csv-lists.py

46 lines
1.3 KiB
Python
Raw Normal View History

2023-10-22 15:34:48 +00:00
#!/usr/bin/python
import argparse
2023-12-29 10:31:27 +00:00
def flatten_and_split(input_string):
elements = input_string.replace("\n", "").split(",")
flat_list = [item.strip() for element in elements for item in element.split("-")]
2023-12-29 10:29:57 +00:00
return ",".join(flat_list)
2023-12-29 10:31:27 +00:00
def combine_and_uniquify(*inputs):
combined_list = [
item.strip().title() for input_list in inputs for item in input_list.split(",")
]
2023-12-29 10:31:27 +00:00
unique_names = set(combined_list)
final_set = set()
for name in unique_names:
2023-10-22 15:34:48 +00:00
parts = name.split()
if len(parts) == 2:
first, last = parts
reversed_name = f"{last} {first}"
2023-12-29 10:31:27 +00:00
# Add the name if its reversed variant is not already in the final set
if reversed_name not in final_set:
final_set.add(name)
2023-10-22 15:34:48 +00:00
else:
2023-12-29 10:31:27 +00:00
final_set.add(name)
return ",".join(sorted(final_set))
2023-10-22 15:34:48 +00:00
def main():
parser = argparse.ArgumentParser(
description="Combine multiple comma-separated lists into one unique sorted list."
2023-10-22 15:34:48 +00:00
)
parser.add_argument("lists", nargs="+", type=str, help="Comma-separated lists.")
2023-10-22 15:34:48 +00:00
args = parser.parse_args()
2023-12-29 10:31:27 +00:00
processed_lists = [flatten_and_split(lst) for lst in args.lists]
result = combine_and_uniquify(*processed_lists)
2023-10-22 15:34:48 +00:00
print(result)
if __name__ == "__main__":
main()