Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from subprocess import call
- import sys
- if len(sys.argv) != 2:
- print("Wrong number of arguments, usage: 'python split.py input_file.csv'")
- sys.exit()
- in_fname = sys.argv[1]
- out_fname = ""
- if len(in_fname) < 6:
- out_fname = in_fname + "_"
- else:
- out_fname = in_fname[0:6] + "_"
- # Reverse lines and store the result in tmp file
- tmp_fname = "_in.tmp"
- tmp_f = open(tmp_fname, "w")
- call(["tac", in_fname], stdout=tmp_f)
- tmp_f.close()
- f_out = None
- prev_year = 0
- with open(tmp_fname) as f:
- for line in f:
- splitted_line = line.rstrip("\n").split(".")
- year = int(splitted_line[0])
- month = int(splitted_line[1])
- if year != prev_year:
- prev_year = year
- if f_out:
- f_out.close()
- f_out = open(out_fname+str(year)+".csv", 'w')
- f_out.write(line)
- f_out.close()
- # Remove the tmp file
- call(["rm", tmp_fname])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement