1

I am trying to make a wage calculator where a user inserts a .txt file and the program calculates the number of hours worked.

So far I am able to separate the names, wage value, and hours, but I can't figure out how to add the hours together.

So my desired result would be:

Names of Employees Wage (how much they make Added number of hours per employee

Here is the data set (file name of txt is -> empwages.txt):

(Edit: the formatting is messed so heres a screen grab of the text: enter image description here

Spencer 12.75   8   8   8   8   10
Ruiz    18  8   8   9.5 8   8
Weiss   14.80   7   5   8   8   10
Choi    15  4   7   5   3.3 2.2
Miller  18  6.5 9   1   4   1
Barnes  15  7.5 9   4   0   2

Desired Outcome:

'Spencer', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes'
'12.75', '18', '14.80', '15', '18', '15'
'42', '41.5', ... and so on

Current code:

infile = open("empwages.txt","r")
masterList = infile.readlines()

nameList = []
hourList = []
plushourList = []
for master in masterList:
    nameList.append(master.split()[0])
    hourList.append(master.split()[1])
    x = 2
    while x <= 6:
        plushourList.append(master.split()[x])
        x += 1


print(nameList)
print(hourList)
print(plushourList)
2
  • Are you opposed to using pandas? This would be a great package to consider for this type of problem. Commented Apr 15, 2019 at 21:01
  • not sure, ultimately i will be putting part of this code into a tkinter GUI imgur.com/a/Ry83ZiB Commented Apr 15, 2019 at 21:02

3 Answers 3

1

It is useful that you get familar with the concept of unpacking a list in Python. You can use the following code to solve your problem:

names = []
hours = []
more_hours = []
with open('empwages.txt') as f:
    for line in f:
        name, hour, *more_hs = line.split()
        names.append(name)
        hours.append(hour)
        more_hours.append(more_hs)

print(*names, sep=', ')
print(*hours, sep=', ')
print(*[sum(float(q) for q in e) for e in more_hours])

In case you need the strings as you have requested:

names = []
hours = []
more_hours = []
with open('empwages.txt') as f:
    for line in f:
        name, hour, *more_hs = line.split()
        names.append(name)
        hours.append(hour)
        more_hours.append(more_hs)

print(more_hours)

names = ', '.join(names)
hours = ', '.join(hours)
more_hours = ', '.join(str(s) for s in [sum(float(q) for q in e) for e in more_hours])


print(names)
print(hours)
print(more_hours)

Output

Spencer, Ruiz, Weiss, Choi, Miller, Barnes
12.75, 18, 14.80, 15, 18, 15
42.0 41.5 38.0 21.5 21.5 22.5
Sign up to request clarification or add additional context in comments.

6 Comments

if I were to implement it into a tkinter gui, how would it be like? please see my imgur for reference. (imgur.com/a/Ry83ZiB)
@jimmyjohn123, what do you mean? I am no following your question.
The code you gave me works perfectly when used in a normal .py file, however, I am trying to implement your code into a gui interface. I am unable to use print(*[sum(float(q) for q in e) for e in more_hours]) as a print statement in the tkinter format.
@jimmyjohn123, got you, you need the string.
@jimmyjohn123, I updated the answer, now you have variables that you can use containing the string, and if you want to contatenate them, just use the string concatenation operator +.
|
1

Try using zip:

with open("empwages.txt") as f:
    lines = [line.split() for line in f]
    names, hours, *more_hours = zip(*lines)
    print(names)
    print(hours)
    print([sum(map(float, i)) for i in zip(*more_hours)])

('Spencer', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes')
('12.75', '18', '14.80', '15', '18', '15')
[42.0, 41.5, 38.0, 21.5, 21.5, 22.5]

This will:

  • Split the file up by line, and split the lines up by word
  • Put the first word of each line in names, the second in hours, and the rest in more_hours

You can add more variables before the *_ as needed.

(Edited to correctly sum hours).

Comments

0

Well if you're not opposed to using pandas:

import pandas as pd
from StringIO import StringIO
import re

initial_data = '''Spencer 12.75   8   8   8   8   10
Ruiz    18  8   8   9.5 8   8
Weiss   14.80   7   5   8   8   10
Choi    15  4   7   5   3.3 2.2
Miller  18  6.5 9   1   4   1
Barnes  15  7.5 9   4   0   2'''

df = pd.read_csv(StringIO(re.sub(r'[ ]+', ',', initial_data, flags=re.M)), header=None)
print(df)
     0      1    2  3    4    5     6
0  Spencer  12.75  8.0  8  8.0  8.0  10.0
1     Ruiz  18.00  8.0  8  9.5  8.0   8.0
2    Weiss  14.80  7.0  5  8.0  8.0  10.0
3     Choi  15.00  4.0  7  5.0  3.3   2.2
4   Miller  18.00  6.5  9  1.0  4.0   1.0
5   Barnes  15.00  7.5  9  4.0  0.0   2.0

Then you can quickly sum over the columns like so:

df.loc[:, 1:].sum(axis=1)
0    54.75
1    59.50
2    52.80
3    36.50
4    39.50
5    37.50
dtype: float64

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.