Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import csv
  2. from os import listdir
  3. from os.path import isfile, join
  4. import re
  5.  
  6. onlyfiles = [f for f in listdir(".") if isfile(join(".", f)) and f.lower().endswith('.csv')]
  7.  
  8. familyname = []
  9.  
  10. tekname = re.compile('[0-9]{6}_[0-9]{6}')
  11. for f in onlyfiles:
  12. if tekname.match(f):
  13. cutname = f[0:13]
  14. if not cutname in familyname and not isfile(cutname+".csv"):
  15. familyname.append(cutname)
  16.  
  17. print("Going to generate the following: "+str(familyname))
  18. input("Ok? (ctrl+c to stop)")
  19.  
  20. for file in familyname:
  21. print("Generating "+ file)
  22. input_files = [f for f in onlyfiles if file in f]
  23. print("Input files: "+str(input_files))
  24.  
  25. newCSVdata = []
  26. first = True # we need to get x from somewhere!
  27. for f in input_files:
  28. csvfile = open(f)
  29. readCSV = csv.reader(csvfile, delimiter=',')
  30. x = []
  31. y = []
  32. for row in readCSV:
  33. x.append(row[3])
  34. y.append(row[4])
  35.  
  36. if first:
  37. first = False
  38. newCSVdata.append(x)
  39. newCSVdata.append(y)
  40.  
  41. csvfile.close()
  42.  
  43. with open(file+".csv", "w", newline='') as new_csv:
  44. csvWriter = csv.writer(new_csv, delimiter=',')
  45. csvWriter.writerows([*zip(*newCSVdata)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement