Advertisement
lswest

CSV.py

Dec 5th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Script to create a list of addresses from a CSV file
  3. # Author: Lucas Westermann
  4. import csv, sys, getopt, codecs
  5.  
  6. def main(argv):
  7.     addressPos = 40 # elements position containing home address in the CSV file // original: 23
  8.     firstName = 0 # position of first name in csv file // original: 0
  9.     lastName = 2 # position of last name in csv file // original: 2
  10.     namePrefix = 8 # Position of prefix (i.e. Dr.) // original: 8
  11.     street1 = 41 # street address line 1 // original: 24
  12.     street2 = 25 # street address line 2 // original: 25
  13.     city = 42 # city position // original: 28
  14.     postal = 45 # postal code position // original: 30
  15.     country = 46 # country position // original: 31
  16.     otherSpacer = 37 # number of fields to advanced to reach other address // original: 37
  17.     company = 58 # location of company name // original: 42
  18.     jobTitle = 60 # location of job title // original: 43
  19.     department = 44 # department position // original: 44
  20.     businessSpacer = 26 # number of fields to advance to reach business address // original: 26
  21.     contacts = "files/contacts.csv"
  22.     errorlog = open('files/error.log', 'w')
  23.     divider = False
  24.  
  25.     usage="usage: " + argv[0] + " [options] FILE\nOptions:\n-h: this help dialog\n-d: add a divider between each address\n-a: append to address list\n-n: overwrite address list (default)\nFILE: path to address list.  Default: Files/address.txt"
  26.  
  27.     if (not argv[len(argv)-1].startswith("-")) and argv[len(argv)-1] != argv[0]:
  28.         contacts = argv[len(argv)-1] # if file is given, use that for output
  29.  
  30.     if len(argv) >= 2:
  31.         if "-h" in argv:
  32.             print(usage)
  33.             sys.exit()
  34.         elif "-a" in argv:
  35.             print("Append mode active")
  36.             addresslist = open('files/addresses.txt', 'a')
  37.         elif "-d" in argv:
  38.             divider = True
  39.             addresslist = open('files/addresses.txt', 'w')
  40.         else:
  41.             addresslist = open('files/addresses.txt', 'w')
  42.     else:
  43.         addresslist = open('files/addresses.txt', 'w')
  44.  
  45.  
  46.     with open(contacts, 'r', encoding="utf-8") as f:
  47.         lines = csv.reader(f)
  48.         your_list = list(lines)
  49.  
  50.  
  51.     for item in range(1,len(your_list)):
  52.         name = your_list[int(item)][firstName]
  53.         position = your_list[int(item)][jobTitle]
  54.         companyName = your_list[int(item)][company]
  55.         address = your_list[int(item)][street1] + "\n"
  56.         if any(c.isalpha() for c in your_list[int(item)][postal]):
  57.             address += your_list[int(item)][city] +" " + your_list[int(item)][postal] +"\n"
  58.         else:
  59.             address += your_list[int(item)][postal] + " " + your_list[int(item)][city] +"\n"
  60.         address += your_list[int(item)][country]
  61.         if name == "" or address == "":
  62.             print(name)
  63.             addresslist.write(name + '\n')
  64.             if position != "":
  65.                 print(position)
  66.                 addresslist.write(position + "\n")
  67.             if companyName != "":
  68.                 print(companyName)
  69.                 addresslist.write(companyName + "\n")
  70.             print(address + '\n')
  71.             addresslist.write(address + '\n\n')
  72.             if divider:
  73.                 print("------------------------------------------------------------------")
  74.                 addresslist.write("------------------------------------------------------------------\n")
  75.             errorlog.write("Missing value on address #" + str(item)+" (" + name + ", " + position + ", " + companyName + ", " + address + ")")
  76.         else:
  77.             print(name)
  78.             addresslist.write(name + '\n')
  79.             if position != "":
  80.                 print(position)
  81.                 addresslist.write(position + "\n")
  82.             if companyName != "":
  83.                 print(companyName)
  84.                 addresslist.write(companyName + "\n")
  85.             print(address + '\n')
  86.             addresslist.write(address + '\n\n')
  87.             if divider:
  88.                 print("------------------------------------------------------------------")
  89.                 addresslist.write("------------------------------------------------------------------\n")
  90.     errorlog.close()
  91.     addresslist.close()
  92.  
  93. if __name__ == "__main__":
  94.    main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement