1

I'm trying to complete a Project that will show total annual sales from an specific list contained in a .txt file.

The list is formatted this way:

-lastname, firstname (string)
-45.7 (float)
-456.4 (float)
-345.5 (float)
-lastname2, firstname2 (string)
-3354.7 (float)
-54.6 (float)
-56.2 (float)
-lastname3, firstname3 (string)
-76.6 (float)
-34.2 (float)
-48.2 (float)

And so on.... Actually, 7 different "employees" followed by 12 set of "numbers" (months of the year)....but that example should suffice to give an idea of what I'm trying to do.

I need to output this specific information of every "employee" -Name of employee -Total Sum (sum of the 12 numbers in the list)

So my logic is taking me to this conclusion, but I don't know where to start:

Create 7 different arrays to store each "employee" data. With this logic, I need to split the main list into independent arrays so I can work with them.

How can this be achieved? And also, if I don't have a predefined number of employees (but a defined format :: "Name" followed by 12 months of numbers)...how can I achieve this?

I'm sure I can figure once I get an idea how to "split" a list in different sections -- Every 13 lines?

2
  • What about % (modulo) operator? You could use it to increase "output" array index every 13 steps. Commented Dec 1, 2012 at 23:24
  • Just do a while loop and parse the 13 points? Commented Dec 1, 2012 at 23:25

3 Answers 3

3

Yes, at every thirteenth line you'd have the information of an employee.

However, instead of using twelve different lists, you can use a dictionary of lists, so that you wouldn't have to worry about the number of employees.

And you can either use a parameter on the number of lines directed to each employee.

You could do the following:

infile = open("file.txt", "rt")

employee = dict()
name = infile.readline().strip()
while name:

    employee[name] = list()

    for i in xrange(1, 12):
        val = float(infile.readline().strip())
        employee[name].append(val)

    name = infile.readline().strip()

Some ways to access dictionary entries:

for name, months in employee.items():
    print name
    print months

for name in employee.keys():
    print name
    print employee[name]

for months in employee.values():
    print months

for name, months in (employee.keys(), employee.values()):
    print name
    print months

The entire process goes as follows:

infile = open("file.txt", "rt")

employee = dict()
name = infile.readline().strip()
while name:

    val = 0.0
    for i in xrange(1, 12):
        val += float(infile.readline().strip())
    employee[name] = val

    print ">>> Employee:", name, " -- salary:", str(employee[name])
    name = infile.readline().strip()

Sorry for being round the bush, somehow (:

Sign up to request clarification or add additional context in comments.

16 Comments

open("file.txt", "rt") !
Thanks, I've added the r flag, which is optional for input files, but i didn't get the t flag, what is it for?
This seems like the best solution! Thanks @Rubens. I still can't get it to work, are you sure the list will hold str's and numbers? It seems it gives me an error every time I try to run that program with my SalesData.txt
You're welcome! Try to make things in a more generic way, so you may find it easier to think about using the containers. Just don't restrict the size of things to much (: Regards! Yes, the list do store the numbers, what is the error you're receiving?
@Rubens "Explicit is better than implicit.." "t" for text mode, "b" for binary. We read in text mode - so we say this (even if this is not neccesary) for everybody who will read the code.
|
1

Here is option. Not good, but still brute option.

summed = 0
with open("file.txt", "rt") as f:
    print f.readline() # We print first line (first man) 
    for line in f:
        # then we suppose every line is float.
        try:                             
            # convert to float
            value = float(line.strip())  
            # add to sum
            summed += value              
        # If it does not convert, then it is next person
        except ValueError: 
            # print sum for previous person
            print summed
            # print new name
            print line
            # reset sum
            summed = 0
    # on end of file there is no errors, so we print lst result
    print summed

since you need more flexibility, there is another option:

    data = {} # dict: list of all values for person by person name
    with open("file.txt", "rt") as f:
        data_key = f.readline() # We remember first line (first man)
        data[data_key] = [] # empty list of values
        for line in f:
            # then we suppose every line is float.
            try:                             
                # convert to float
                value = float(line.strip())  
                # add to data
                data[data_key].append(value)
            # If it does not convert, then it is next person
            except ValueError: 
                # next person's name
                data_key = line
                # new list
                data[data_key] = []

Q: let's say that I want to print a '2% bonus' to employees that made more than 7000 in total sales (12 months)

for employee, stats in data.iteritems():
    if sum(stats) > 7000:
        print employee + " done 7000 in total sales! need 2% bonus"

8 Comments

This is perfect!! From here I think I can manage to use the data as I need to. Thank youuu again!! you guys are so helpful here
@mmmaceo It is not good (and a little ugly) because of many thrown exceptions. On big data it may run slow. But for 10-100 persons I think it's ok.
@mmmaceo added another option :)
It works perfectly. Might be a little ugly, but for a list of 10 or so, it works perfectly! I hate to just keep elaborating once the asked question is resolved...but! I got the print output perfectly, let's say that I want to print a '2% bonus' to employees that made more than 7000 in total sales (12 months). Is there a way to access each of the 'employees' independently? Or analyze the sum(months) and when the result of it is more than 3000 (in an specific employee) it will print '2% bonus'. I tried: if sum(months) > 3000 -- print 'bonus' -- but affects all, not specific employees
@mmmaceo I've added answer to this question (as i understood it). When you upvote me? :D
|
0

I would not create 7 different arrays. I would create some sort of data structure to hold all the relevant information for one employee in one data type (this is python, but surely you can create data structures in python as well).

Then, as you process the data for each employee, all you have to do is iterate over one array of employee data elements. That way, it's much easier to keep track of the indices of the data (or maybe even eliminates the need to!).

This is especially helpful if you want to sort the data somehow. That way, you'd only have to sort one array instead of 7.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.