Created
June 29, 2013 00:31
-
-
Save codebynumbers/5889116 to your computer and use it in GitHub Desktop.
Split data into multiple files
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
| import sys | |
| path = sys.argv[1] | |
| ext = 'txt' | |
| dir = 'output' | |
| limit = 10000 | |
| limits = {} | |
| with open(path) as file: | |
| for line in file: | |
| try: | |
| (country, keyword, count) = line.strip().split("\t") | |
| except: | |
| continue | |
| if len(country) != 2: | |
| continue | |
| if limits.get(country, 0) < limit: | |
| with open("%s/%s.%s" % (dir, country, ext), 'a') as output: | |
| output.write(keyword) | |
| output.write("\n") | |
| limits[country] = limits.get(country, 0) + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment