View Single Post
Old 12-10-2012, 01:37 PM   #2
ucpete
Member
 
Location: San Francisco Bay Area

Join Date: Dec 2008
Posts: 35
Default

Here's some Python code to do it for you, assuming your file is called data.txt:

Code:
inputFile = open("data.txt")
inputFile.next() # skip that first header line
frequencyDict = {}
for line in inputFile:
    curLength = int(line.strip().split(',')[2])
    if curLength in frequencyDict:
        frequencyDict[curLength] += 1
    else:
        frequencyDict[curLength] = 1
input.close()

outputFile = open("data_transformed.txt",'w')
outputFile.write("Length, Quantity\n")
minLength, maxLength = min(frequencyDict.keys()), max(frequencyDict.keys())
for i in range(minLength,maxLength+1):
    if i in frequencyDict:
        outputFile.write("%s, %s\n" % (str(i),str(frequencyDict[i])))
outputFile.close()
You could write it more efficiently with try-except statements, but that's about as clear of an implementation there is IMO and it'll be fast anyway.
ucpete is offline   Reply With Quote