I just came across this question. All the solutions given are in PERL for this question. I thought I can learn solving this in Python as I could learn many thing by solving this problem. But I am stuck. The question is:

You are working at a grocery store which tracks its customers' purchasing habits
by scanning frequent shoppers' cards. Your cash registers logs all transactions
to a pipe-delimited ("|") flat text file in the following format:

An example log file could be:

Write a script/program (in your language of choice) that will parse the log and
generate the following output:

A report of the total revenue resulting from each customer.
A report for each customer showing how much of their spending went to each category.
With the above example log file, the script should output should be something like:

Total Revenue:
Pedro - $11.02
Nitin - $15.00
Susie - $70.15

Purchases by Pedro:
groceries - $1.42
fuel - $9.60

Purchases by Nitin:
tobacco - $15.00

Purchases by Susie:
groceries - $10.25
fuel - $44.90
tobacco - $15.00
quare have?:

I just worte a piece of code to read the "|" delimeted file. but I don't know how to procedd further like how to group them in category and solve this problem. Can anyone explain me the idea of how to solve this.`Inline Code Example Here

def filefunc():

    file_input = raw_input("Enter the name of the file: ")
    file_open = open(file_input, "r")

    file_read = file_open.readline()
    i = 1
    while file_read:
        print file_read,
        file_read = file_open.readline()
        i = i + 1

    # print file_read


4 Years
Discussion Span
Last Post by pyTony

I just know how to read the contents of the file. I wanted to know how to proceed inorder to group the contents based on categories. I think I can use split method to split the pipe-line from the contents. but then again im stuck with content grouping :(

Edited by prashanthmukund: wanted to be more specific


What you did can be done as:

def filefunc():
    with open(raw_input("Enter the name of the file: ")) as infile:

Yes you definitely need to use split and save the data in list or probably best would be dictionary of dictionaries. But you could be clever and sort the file lines first (for line in sorted(infile):), then you can just add and print as you read the lines

Edited by pyTony


You could also read the file and build a dict mapping pairs like ('Pedro', 'groceries') to the total cost for Pedro in this category (the float 1.42). So the dictionary would look like

    ('Pedro', 'groceries'): 1.42,
    ('Nitin', 'tobacco'): 15.0,
    ('Susie', 'groceries'): 10.25,

Edited by Gribouillis


I did little under 25 lines of code program by sorting method, I must report the total sales in the end after reading all lines, not first, as I do not save category sales, but only print them on the fly.

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.