Guest User

Untitled

a guest
Feb 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. # encoding: utf-8
  2. """This is to parse the results from multiple files and pull them into one file"""
  3. from os import listdir
  4. from string import split
  5.  
  6. PARSE_DIR = "C:/Users/jhaagsma/Dropbox (Personal)/JulianChrisShare/EIS/eisSaltData/2018-02-01/CaCl10wt"
  7. PREPEND_OF_INTEREST = 'phaseinfo'
  8. SUMMARY_FILE = 'summary.info.csv'
  9.  
  10. # get the files in the dir
  11. PARSE_DIR = PARSE_DIR.rstrip("/") + '/'
  12. files = listdir(PARSE_DIR)
  13. wants = []
  14.  
  15. for f in files:
  16. if f.startswith(PREPEND_OF_INTEREST):
  17. # add the file to our list...
  18. wants.append(f)
  19.  
  20. print("There are {} files matching {}".format(len(wants), PREPEND_OF_INTEREST))
  21.  
  22. if len(wants) <= 0:
  23. # exit the program
  24. exit(0)
  25.  
  26. filepath = PARSE_DIR + SUMMARY_FILE
  27.  
  28. print("Saving to: {}".format(filepath))
  29.  
  30. fhandle = open(filepath, 'w')
  31. fhandle.write("Date,Time,Unix,Name,Frequency,"
  32. "Z-Magnitude,Z-Phase,Z-Real,Z-Imaginary,TemperatureStart,TemperatureEnd,Resistor,\n")
  33.  
  34. for f in wants:
  35. name = date = time = unix = freq = zp = zx = zy = ts = te = r = None
  36. zm = -1
  37. fo = open(PARSE_DIR + f, 'r')
  38. line = "blah"
  39.  
  40. for line in fo:
  41. if not line:
  42. break
  43.  
  44. try:
  45. index, value = line.split(': ')
  46. except ValueError:
  47. break
  48.  
  49. value = value.strip()
  50.  
  51. if index == 'SampleName':
  52. name = value
  53. elif index == 'Date':
  54. date = value
  55. elif index == 'Unix':
  56. unix = value
  57. elif index == 'Frequency':
  58. freq = value
  59. elif index == 'Resistor':
  60. r = value
  61. elif index == 'Z_magnitude':
  62. zm = value
  63. elif index == 'Z_phase':
  64. zp = value
  65. elif index == 'Z_x':
  66. zx = value
  67. elif index == 'Z_y':
  68. zy = value
  69. elif index == 'StartTemperature':
  70. ts = value
  71. elif index == 'EndTemperature':
  72. te = value
  73.  
  74. if date and time and unix and zm is not -1 and ts and te and r:
  75. break
  76.  
  77. csvstring = "{},{},{},{},{},{},{},{},{},{},{},{}\n".format(date, time, unix, name, freq, zm, zp, zx, zy, ts, te, r)
  78. fhandle.write(csvstring)
  79. print("Wrote from {}".format(f))
  80.  
  81. print("Finished writing")
  82. exit(0)
Add Comment
Please, Sign In to add comment