scripts/merge-csv-lists.py

56 lines
1.7 KiB
Python
Raw Normal View History

2023-10-22 15:34:48 +00:00
#!/usr/bin/python
import argparse
2023-12-29 10:29:57 +00:00
def split_and_flat(input_string):
if "\n" in input_string:
_without_newlines = input_string.split("\n")
_without_empty = filter(lambda x: x != "", _without_newlines)
input_string = ",".join(_without_empty)
comma_split = input_string.split(",")
flat_list = []
for item in comma_split:
dash_split = item.split("-")
flat_list.extend([value.strip().replace("\n", "") for value in dash_split])
return ",".join(flat_list)
def unique_combined_list(*inputs):
combined_list = [
item.strip().title() for input_list in inputs for item in input_list.split(",")
]
2023-10-22 15:34:48 +00:00
final_list = []
for name in combined_list:
parts = name.split()
if len(parts) == 2:
first, last = parts
reversed_name = f"{last} {first}"
if name not in final_list and reversed_name not in final_list:
final_list.append(name)
else:
if name not in final_list:
final_list.append(name)
sorted_list = sorted(final_list)
2023-10-22 15:34:48 +00:00
output = ",".join(sorted_list)
return output
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()
if len(args.lists) == 1:
args.lists.append(args.lists[0])
2023-12-29 10:29:57 +00:00
# Process each list with split_and_flat if it contains a dash
processed_lists = [split_and_flat(lst) if "-" in lst else lst for lst in args.lists]
2023-10-22 15:34:48 +00:00
2023-12-29 10:29:57 +00:00
result = unique_combined_list(*processed_lists)
2023-10-22 15:34:48 +00:00
print(result)
if __name__ == "__main__":
main()