Newbie with sort text file question
Behrang Dadsetan
ben at dadsetan.com
Sun Jul 13 16:57:05 EDT 2003
stuartc wrote:
> Hi:
>
> Here's the text file:
>
> banana_c \\yellow
> apple_a \\green
> orange_b \\yellow
> banana_d \\green
> orange_a \\orange
> apple_w \\yellow
> banana_e \\green
> orange_x \\yellow
> orange_y \\orange
>
> I would like two output files:
>
> 1) Sorted like this, by the fruit name (the name before the dash)
>
> 2) Then summarized like this, ordered with the highest occurances
> first:
Here is some mostly tested code ;)
import re
file = open ("textfile.txt") # your file name instead of textfile.txt
alllines = list(file.readlines())
file.close()
alllines.sort()
fruitre = re.compile('^[a-z]+')
fruits = {}
for line in alllines:
fruitresult = fruitre.search(line)
print line
if fruitresult:
fruit = fruitresult.group(0)
fruits.setdefault(fruit, 0)
fruits[fruit] += 1
totalamount = 0
for fruit, amount in fruits.items():
print fruit, " occurs ", amount
totalamount += amount
print "Total amount of fruits ", totalamount
Regards, Ben.
PS: It looks a little unoptimized to me but it works. Hopefully others
will reply to you as well so I can learn how to make the above better.
More information about the Python-list
mailing list