Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Script to create a list of addresses from a CSV file
- # Author: Lucas Westermann
- import csv, sys, getopt, codecs
- def main(argv):
- addressPos = 40 # elements position containing home address in the CSV file // original: 23
- firstName = 0 # position of first name in csv file // original: 0
- lastName = 2 # position of last name in csv file // original: 2
- namePrefix = 8 # Position of prefix (i.e. Dr.) // original: 8
- street1 = 41 # street address line 1 // original: 24
- street2 = 25 # street address line 2 // original: 25
- city = 42 # city position // original: 28
- postal = 45 # postal code position // original: 30
- country = 46 # country position // original: 31
- otherSpacer = 37 # number of fields to advanced to reach other address // original: 37
- company = 58 # location of company name // original: 42
- jobTitle = 60 # location of job title // original: 43
- department = 44 # department position // original: 44
- businessSpacer = 26 # number of fields to advance to reach business address // original: 26
- contacts = "files/contacts.csv"
- errorlog = open('files/error.log', 'w')
- divider = False
- 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"
- if (not argv[len(argv)-1].startswith("-")) and argv[len(argv)-1] != argv[0]:
- contacts = argv[len(argv)-1] # if file is given, use that for output
- if len(argv) >= 2:
- if "-h" in argv:
- print(usage)
- sys.exit()
- elif "-a" in argv:
- print("Append mode active")
- addresslist = open('files/addresses.txt', 'a')
- elif "-d" in argv:
- divider = True
- addresslist = open('files/addresses.txt', 'w')
- else:
- addresslist = open('files/addresses.txt', 'w')
- else:
- addresslist = open('files/addresses.txt', 'w')
- with open(contacts, 'r', encoding="utf-8") as f:
- lines = csv.reader(f)
- your_list = list(lines)
- for item in range(1,len(your_list)):
- name = your_list[int(item)][firstName]
- position = your_list[int(item)][jobTitle]
- companyName = your_list[int(item)][company]
- address = your_list[int(item)][street1] + "\n"
- if any(c.isalpha() for c in your_list[int(item)][postal]):
- address += your_list[int(item)][city] +" " + your_list[int(item)][postal] +"\n"
- else:
- address += your_list[int(item)][postal] + " " + your_list[int(item)][city] +"\n"
- address += your_list[int(item)][country]
- if name == "" or address == "":
- print(name)
- addresslist.write(name + '\n')
- if position != "":
- print(position)
- addresslist.write(position + "\n")
- if companyName != "":
- print(companyName)
- addresslist.write(companyName + "\n")
- print(address + '\n')
- addresslist.write(address + '\n\n')
- if divider:
- print("------------------------------------------------------------------")
- addresslist.write("------------------------------------------------------------------\n")
- errorlog.write("Missing value on address #" + str(item)+" (" + name + ", " + position + ", " + companyName + ", " + address + ")")
- else:
- print(name)
- addresslist.write(name + '\n')
- if position != "":
- print(position)
- addresslist.write(position + "\n")
- if companyName != "":
- print(companyName)
- addresslist.write(companyName + "\n")
- print(address + '\n')
- addresslist.write(address + '\n\n')
- if divider:
- print("------------------------------------------------------------------")
- addresslist.write("------------------------------------------------------------------\n")
- errorlog.close()
- addresslist.close()
- if __name__ == "__main__":
- main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement