Command line proejct with python
Anonymous in /c/coding_help
491
report
I am trying to use argparse and python to create a program that will convert files from one type to another. <br><br>like<br><br>python filename.py -c bscour5916.txt csv<br><br>but I get a whole batch of errors, and I am not sure if this is good or not. <br><br>Below is what I have so far<br><br>```python<br>import argparse<br>import time<br>import csv<br>import json<br><br>def to_csv(input_string):<br> output = {"Date": [], "Name": [], "Amount": [], "Category": []}<br> current_date = None<br> for line in input_string:<br> if len(line) > 12:<br> if line[:11] == "Transaction":<br> output["Date"] += [line.split(": ")[1]]<br> output["Name"] += [line.split(": ")[2]]<br> output["Amount"] += [line.split(": ")[3]]<br> output["Category"] += [line.split(": ")[4]]<br> else:<br> print("bad line: ", line)<br> return output<br><br><br>def main():<br> parser = argparse.ArgumentParser()<br> parser.add_argument("-c", action="store", dest="c")<br> args = parser.parse_args()<br><br> text = open(args.c, "r").readlines()<br><br> output = to_csv(text)<br><br> with open(f'{time.time()}.csv', 'w', newline='') as file:<br> writer = csv.writer(file)<br> writer.writerow(["Date", "Name", "Amount", "Category"])<br> for x in range(len(output["Date"])):<br> writer.writerow([output["Date"][x], output["Name"][x], output["Amount"][x], output["Category"][x]])<br><br><br>if __name__ == "__main__":<br> main()<br>```<br><br>any help would be great thanks.
Comments (9) 14890 👁️