Advertisement
maxdimples

Scourgify task u/ThinkSuccotash

Dec 6th, 2022
39
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import csv
  2. import sys
  3.  
  4. if len(sys.argv)>3:
  5.         sys.exit("Too many command-line arguments")
  6. elif len(sys.argv)<3:
  7.         sys.exit("Too few command-line arguments")
  8. else:
  9.     try:
  10.         inputfile=sys.argv[1]
  11.         outputfile=sys.argv[2]
  12.  
  13.         with open(outputfile, 'w') as f:
  14.             header=['first', 'last', 'house']
  15.             writer=csv.DictWriter(f, fieldnames=header)
  16.             writer.writeheader()
  17.  
  18.             with open(inputfile) as csvfile:
  19.                 reader = csv.DictReader(csvfile)
  20.                 for row in reader:
  21.                         # print(row['name'], row['house'])
  22.                     lastname, firstname = row["name"].split(",")
  23.                     house = row['house']
  24.                         # print(firstname)
  25.                     writer.writerow({'first':firstname.lstrip(), 'last':lastname, 'house':house })
  26.  
  27.     except FileNotFoundError:
  28.         sys.exit("Could not read " + inputfile)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement