Skip to content

Instantly share code, notes, and snippets.

@shanemhansen
Created September 11, 2015 20:31
Show Gist options
  • Save shanemhansen/1c6d6f28d7dd13327a8b to your computer and use it in GitHub Desktop.
Save shanemhansen/1c6d6f28d7dd13327a8b to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------------------------
# Name: BusinessData Sorting
# Purpose:
#
# Author: woswald
#
# Created: 01/09/2015
# Copyright: (c) woswald 2015
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import csv
#open csv for reading and read file
f = open(r"D:\Whitney\data 050214\staXX001combined.txt", 'r')
#find each unique items in column 38, create a new empty file for each unique item
#read each row of data, look specifically at the 38th column, depending on the entry in this column sort the data
# for that row to that specific file
myDir = r'D:/Whitney/data 050214/SortedData/'
files = {}
header = f.readline()
csvreader = csv.reader(f)
for line in csvreader:
if line[37] not in files:
# TODO this may need to be a csvwriter
files[line[37]] = open(myDir + line[37].replace("/","_") +".txt", "w")
files[line[37]].write(header)
files[line[37]].write(",".join(line))
files[line[37]].write("\n")
for key in files:
files[key].close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment