Skip to content

Instantly share code, notes, and snippets.

@drjwbaker
Forked from melvinwevers/splitPerYear
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drjwbaker/5380cc6fab97ff18aafd to your computer and use it in GitHub Desktop.
Save drjwbaker/5380cc6fab97ff18aafd to your computer and use it in GitHub Desktop.
SplitperYear
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