Advertisement
Guest User

Untitled

a guest
May 26th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. from subprocess import call
  2. import sys
  3.  
  4. if len(sys.argv) != 2:
  5.     print("Wrong number of arguments, usage: 'python split.py input_file.csv'")
  6.     sys.exit()
  7.  
  8. in_fname = sys.argv[1]
  9.  
  10. out_fname = ""
  11. if len(in_fname) < 6:
  12.     out_fname = in_fname + "_"
  13. else:
  14.     out_fname = in_fname[0:6] + "_"
  15.  
  16. # Reverse lines and store the result in tmp file
  17. tmp_fname = "_in.tmp"
  18. tmp_f = open(tmp_fname, "w")
  19. call(["tac", in_fname], stdout=tmp_f)
  20. tmp_f.close()
  21.  
  22. f_out = None
  23. prev_year = 0
  24.  
  25. with open(tmp_fname) as f:
  26.     for line in f:
  27.         splitted_line = line.rstrip("\n").split(".")
  28.         year = int(splitted_line[0])
  29.         month = int(splitted_line[1])
  30.         if year != prev_year:
  31.             prev_year = year
  32.             if f_out:
  33.                 f_out.close()
  34.             f_out = open(out_fname+str(year)+".csv", 'w')
  35.         f_out.write(line)
  36.     f_out.close()
  37.  
  38. # Remove the tmp file
  39. call(["rm", tmp_fname])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement