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:
customer_name|product_category|item_description|cost

An example log file could be:

Pedro|groceries|apple|1.42
Nitin|tobacco|cigarettes|15.00
Susie|groceries|cereal|5.50
Susie|groceries|milk|4.75
Susie|tobacco|cigarettes|15.00
Susie|fuel|gasoline|44.90
Pedro|fuel|propane|9.60
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
    file_open.close()
filefunc()

`

I do not see connection of your code with problem.

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 :(

What you did can be done as:

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

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

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,
    ...
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.