954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with File I/O

What I'm trying to do is read a line from a file containing some data that includes a person's first and last name, their hourly wage and the number of hours they worked, and then writing to a seperate file their last name, first name: wage.

The input file looks something like this:

First Name 15.00 40.00
New Name 12.75 38.50

The output file should read something like this:

Name, First: $600.00
Name, New: $490.86

Easy peasy, right? Everything seems to be in working order, except, that when it writes to the file, it writes it four times!

def fileInOut():

    file_input = open('input.txt', 'r')
    file_output = open('output.txt', 'w')

    info = file_input.readline()

    while info != '':

        payroll = info.split()

        first = payroll[0]
        last = payroll[1]
        payrate = float(payroll[2])
        hours = float(payroll[3])
        wage = payrate*hours

        print(last, ',', first, ':', '$', wage)

        for line in payroll:
            file_output.write(last + ', ')
            file_output.write(first + ': ')
            file_output.write('${0: .2f}'.format(wage) + '\n')

        info = file_input.readline()

    file_output.close()
    file_input.close()

fileInOut()


Print output is fine:

>>>>
Name, First: $600.00
Name, New: $490.86
>>>>

But the output.txt is

Name, First: $600.00
Name, First: $600.00
Name, First: $600.00
Name, First: $600.00
Name, New: $490.86
Name, New: $490.86
Name, New: $490.86
Name, New: $490.86

Why is it writing four times??????????????

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

UPDATE: I've been instructed to use value returning functions in my program to calculate the wage.

I have attempted to do so, except now it only calculates the paywage from the first line of input.

def fileInOut():

    file_input = open('input.txt', 'r')
    file_output = open('output.txt', 'w')

    info = file_input.readline()

    while info != '':

        payroll = info.split()

        first = payroll[0]
        last = payroll[1]
        payrate = getPay()
        hours = getHours()
        wage = getWage(payrate, hours)

        print(last, ',', first, ':', '$', wage)

        for line in payroll:
            file_output.write(last + ', ')
            file_output.write(first + ': ')
            file_output.write('${0: .2f}'.format(wage) + '\n')

        info = file_input.readline()

    file_output.close()
    file_input.close()

def getPay():

    pay_input = open('input.txt', 'r')

    rate = pay_input.readline()

    while rate != '':
        pay = rate.split()

        payrate2 = float(pay[2])
        return payrate2

        rate = pay_input.readline()

    pay_input.close()

def getHours():

    hours_input = open('input.txt', 'r')

    hourly = hours_input.readline()

    while hourly != '':
        time = hourly.split()

        hours2 = float(time[3])
        return hours2

        hourly = hours_input.readline()

    hours_input.close()

def getWage(payrate, hours):

    wage2 = payrate*hours
    return wage2

fileInOut()


prints
>>>>
Name,First: $600.00
Name, New: $600.00 (supposed to be 490.86)
>>>>

And it still writes everything four times.

I'd greatly appreciate if someone could first help me figure out why it's writing everything four times, and then how I can loop to the next line with my value returning functions.

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

You are not passing the line to your function and common idiom for inputting lines from file is:

with open('myfile.txt') as infile:
      for line in infile:
          #processing it


time is not recommended name for variable name as it is standard library module name.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

thanks. i actually already edited a few times because of using names that were already functions/modules/etc, (like input)

i didn't use the cleaner open/read because in my course we haven't gotten that far

you said i'm not passing the line to my function... which function would i be passing it to? also, any idea about why it writes 4 lines?

thanks for the advice!

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Use for loop as I showed then and pass the line to your processing function.

infile = open('myfile.txt')
for line in infile:
    # processing it
    print(format_result(calculate(line)))
infile.close()

reposted, because editing time expired

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I'm not getting it, I try using your loop and it just iterates an infinite number of times so that I have to manually close IDLE. It doesn't matter too much at this point, I already failed this particular assignment. I still want to know, though, because our final is this week and we are using some of the same skills.

If it isn't too much trouble, please just show me WHERE to use the loop, in context with what I've posted. (Preferably in the one with the value returning functions.

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

OK, I trust you. Banging your head too long to wall is also not so healthy. You are right, this is fairly basic pattern you must learn well.

Here code expanded with your data file, you can think how to apply yourself:

def calculate(line):
    splitted = line.split()
    if len(splitted) != 4:
        print('Warning line %s, length splitted %i omitted!' % (line.rstrip(), len(splitted)))
        return
    first, family, hourly, hours = splitted
    hourly, hours = float(hourly), float(hours)
    return family, first, hourly * hours
        
def format_result(result):
    return ('%s, %s: %8.2f\n' % result).rjust(30)

if __name__ == '__main__':
    infile = open('input.txt')
    outfile = open('output.txt', 'w')

    for line in infile:
        # processing it
        result = calculate(line)
        if result:
            outfile.write(format_result(result))

    outfile.close()
    infile.close()
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

why did you use if len(length) != 4 and why as a value returning function???

also what are _name_ and '_main_' referencing????

other than that, i see what you're doing

it's frustrating because we've never passed value returning functions to loops, and even though i'm supposed to understand the principle behind it, i cannot figure out the phrasing of the code to do it... what's more is that the program doesn't even NEED a value returning function (as i proved with my first function), but alas, in class, the instructor is the be all end all... i think she might give me a passing grade for my valiant attempt :-)

our Final Project doesn't include such specific instructions, so i'll be able to use a math module (or perhaps my own custom module) instead to make life easier on me

after i complete my final, i may post it on here just to compare my results with what you geniuses come up with :-)

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

length check is to give some basic real word robustness, as it is common practise also last line to include newline, so last line is empty. Same time we can do check to ensure that multiple assignment have correct number on right hand side instead of checking only >0. The idiom to check if name is '__main__' is common idiom to separate out from module the part not used when importing as module. It can serve as main function role, for running the test case tests or timings.

And by the way functions, modules, classes... are programmer's best friend, including yours.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

thank you for that explanation, it proved very useful

however, regarding _name_ and _main_, i was asking literally what they should be referencing, because i can't just put _name_ and _main_ in my function, it'll just tell me that name isn't called

am i missing somethere here? i'm assuming i substitue something for name and main, but i don't know what to substitute... am i way off base?

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

It is two underscores each side, the private system variable convention. __name__ is the name of file as it is imported but __main__ when run directly.
this is how it looks in my smart phone.

Python 2.5.4 (r254:67916, Nov  6 2009, 04:18:57) [C] on symbian_s60
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> __name__
'__main__'
>>>
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

lol i have no idea what you're talking about so im just gonna mark thus solved i dont need to know the answer anymore anyway

pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

HERE'S THE SOLUTION

def fileInOut():

    file_input = open('input.txt', 'r')
    file_output = open('Chandler.txt', 'w')

    info = file_input.readline()

    while info != '':

        payroll = info.split()

        first = payroll[0]
        last = payroll[1]
        payrate = float(payroll[2])
        hours = float(payroll[3])
        wage = payrate*hours

        print(last, ',', first, ':', '$', wage)

        file_output.write(last + ', ')
        file_output.write(first + ': ')
        file_output.write('${0: .2f}'.format(wage) + '\n')

        info = file_input.readline()

    file_output.close()
    file_input.close()

fileInOut()
pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

And here's the solution that meets the value returning and print function parameters.

file_input = open('input.txt', 'r')
file_output = open('outfile.txt', 'w')

def fileInOut():

    info = file_input.readline()

    while info != '':

        payroll = info.split()

        first = payroll[0]
        last = payroll[1]
        payrate = float(payroll[2])
        hours = float(payroll[3])
        wage = getWage(payrate, hours)

        getWage(payrate, hours)
        fileOut(first, last, str(format(wage, '.2f')))

        print(last, ',', first, ':', '$', wage)

        info = file_input.readline()

    file_input.close()
    file_output.close()

def getWage(P, H):

    wage2 = P*H
    return wage2

def fileOut(F, L, W):

    file_output.write(L + ', ')
    file_output.write(F + ': $')
    file_output.write(W + '\n')

fileInOut()
pythonstudent11
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: