parse-vcf.py 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/python3
  2. import os
  3. import glob
  4. # Open (create if not exists) file, truncate and write csv header
  5. csv=open("book.csv","w+")
  6. csv.truncate(0)
  7. csv.write('org;fullname;street;city;zip\n')
  8. # Parse all contacts
  9. for file in glob.glob('./contacts/*/*.vcf'):
  10. # Empty variables to get rid of relicts from previous files
  11. fn = po_box = ext_address = street = city = region = zip = country = org = ""
  12. for line in open(file):
  13. if "FN:" in line:
  14. print(file)
  15. print(line)
  16. fn=line.rstrip().replace("FN:","")
  17. continue
  18. if "ADR;" in line:
  19. print(line)
  20. adr=line.rstrip().split(':', 1)[-1].split(';')
  21. po_box=adr[0]
  22. ext_address=adr[1]
  23. street=adr[2]
  24. city=adr[3]
  25. region=adr[4]
  26. zip=str(adr[5])
  27. country=adr[6]
  28. continue
  29. if "ORG:" in line:
  30. print(line)
  31. org=str(line.rstrip().replace("ORG:","").split(';')[0])
  32. continue
  33. if len(org) > 0:
  34. csv.write(org+';'+fn+';'+street+';'+city+';'+zip+'\n')
  35. csv.close()