Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import re
  2. import json
  3.  
  4. def pos_float(s):
  5. if s[-1]=="-":
  6. return -float(s[:-1])
  7. else:
  8. return float(s)
  9.  
  10. def parse_entry(entry):
  11. header_re = r"\s+DATE\s+TIME\s+TERM\s+TRANS\s+OPER\s+GROSS\+\s+GROSS-\s+NET\s+TRAN TYPE\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+"
  12. match = re.search(header_re, entry)
  13. if match is None:
  14. return None
  15. transinfo = match.groups()
  16.  
  17. item_re = r"^( \w+)?\s+(([\w%]+ ?)+)\s+(\d+\.\d+-?)\s+Dept\s+(\d+)"
  18. items = re.findall( item_re, entry, flags=re.MULTILINE )
  19.  
  20. ret = {'date':transinfo[0],
  21. 'time':transinfo[1],
  22. 'term':int(transinfo[2]),
  23. 'trans':int(transinfo[3]),
  24. 'oper':int(transinfo[4]),
  25. 'gross+':pos_float(transinfo[5]),
  26. 'gross-':pos_float(transinfo[6]),
  27. 'net':pos_float(transinfo[7]),
  28. 'type':transinfo[8]}
  29.  
  30. items = [(x[0].strip(), x[1].strip(), pos_float(x[3]), int(x[4])) for x in items]
  31.  
  32. ret['items'] = items
  33.  
  34. return ret
  35.  
  36. def parse_transaction_file(fn):
  37. raw = open(fn, encoding="latin1").read()
  38.  
  39.  
  40. # remove page headers
  41. headerre = re.compile( r" +Auto Report: (\b.*)\s+Entry: (\b.*)\s+TRANSACTION SUMMARY LOG REPORT - STORE\s+(.+)\s+PREVIOUS PERIOD - (\S+)\s+Reported at:\s+(\S+ \S+)\s+",
  42. flags=re.MULTILINE)
  43. headerless, ct = re.subn(headerre, "", raw)
  44.  
  45. # remove page footers
  46. pageless, ct = re.subn(r'\n +Page \d+.*\n', "\n", headerless)
  47.  
  48. # split at ======
  49. entries = re.split(r"\n=+\n", pageless)
  50.  
  51. trans = []
  52. for i, entry in enumerate( entries ):
  53. x = parse_entry(entry)
  54. if x is not None: trans.append(x)
  55.  
  56. return trans
  57.  
  58. def pos_to_json(fn_in, fn_out):
  59. trns = parse_transaction_file(fn_in)
  60.  
  61. fpout = open(fn_out,"w")
  62. for trn in trns:
  63. fpout.write( json.dumps(trn) )
  64. fpout.write("\n")
  65. fpout.close()
  66.  
  67. if __name__=='__main__':
  68. import sys
  69.  
  70. if len(sys.argv)<3:
  71. print( "usage: %s transaction_filename output_filename"%sys.argv[0] )
  72. exit()
  73.  
  74. fn_in = sys.argv[1]
  75. fn_out = sys.argv[2]
  76.  
  77. pos_to_json(fn_in, fn_out)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement