Created
March 11, 2015 10:04
-
-
Save melvinwevers/9e550dc623b8a66ddff3 to your computer and use it in GitHub Desktop.
SplitperYear
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src_path = "" #here you need to input the directory that contains the file | |
main_file = "" #here you need to input the name of the file | |
import csv | |
import collections | |
import pprint | |
with open(main_file, "rb") as fp: | |
root = csv.reader(fp, delimiter='\t') | |
result = collections.defaultdict(list) | |
for row in root: | |
year = row[3].split("-")[0] #change the row number if the dates are in a different column | |
result[year].append(row) | |
print "Result:-" | |
pprint.pprint(result) | |
for i,j in result.items(): | |
file_path = "%s%s.csv"%(src_path, i) | |
with open(file_path, 'wb') as fp: | |
writer = csv.writer(fp, delimiter='\t') | |
writer.writerows(j) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment