4 Replies - 653 Views - Last Post: 29 March 2012 - 12:09 PM Rate Topic: -----

#1 Coconutmilk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 12

Input/Output file help

Posted 29 March 2012 - 08:47 AM

My question has to do with the input for files. In my input file, I have a series of numbers on separate lines that need to be converted to Fahrenheit and Kelvin. My problem is with assigning variables to each line of the input file so I can do this.
def main():
    infileName = input("What file are the temperatures in? ")
    outfileName = input("What file should the temperatures go? ")

    #opens the files
    infile = open(infileName, "r")
    outfile = open(outfileName, "w")

    #process each line of the input file
    for line in infile:
        eval(Line 1) = x 
        celsius = x
        fahrenheit = 9/5 * celsius + 32
        kelvin = celsius + 273.15
        eval(Line 2) = y 
        celsius = y
        fahrenheit = 9/5 * celsius + 32
        kelvin = celsius + 273.15
        print("Iteration #:", "Celsius:",celsius, "Fahrenheit:",fahrenheit,"Kelvin:",kelvin, file=outfile)

main()
         

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Input/Output file help

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Input/Output file help

Posted 29 March 2012 - 09:00 AM

mind posting an example of the input file? I think what you want to do is read the entire file and put each line into a list.

This post has been edited by atraub: 29 March 2012 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 Coconutmilk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 12

Re: Input/Output file help

Posted 29 March 2012 - 09:04 AM

Surely
They are just simple numbers on each line
4
0
55
100
-90
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Input/Output file help

Posted 29 March 2012 - 11:05 AM

Firstly, I think you mean:

x = int(Line 1)
...
y = int(...)



Assigning to a line of text file doesn't make sense, it's the other way around. It's better just to cast them to an integer rather than evaluating. Are you trying to read the second line of input for y? It is still in the for line in infile: loop processing the line read for x.

If you want a different value for y you need to read another line of the input, maybe you could explain exactly what you want to happen, and what is happening now?
Was This Post Helpful? 0
  • +
  • -

#5 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Input/Output file help

Posted 29 March 2012 - 12:09 PM


for line in infile:
        eval(Line 1) = x 
        celsius = x
        fahrenheit = 9/5 * celsius + 32
        kelvin = celsius + 273.15
        eval(Line 2) = y 
        celsius = y
        fahrenheit = 9/5 * celsius + 32
        kelvin = celsius + 273.15
        print("Iteration #:", "Celsius:",celsius, "Fahrenheit:",fahrenheit,"Kelvin:",kelvin, file=outfile)




im just curious why you are doing the same thing twice with in this for loop? also you should prolly do x = eval(line) not eval(Line1) = x. This is because you cant set eval(Line1) to anything also, Line1 isnt defined anywhere, and line (i believe) is holding the line from your inputfile. Also you should define x outside the for loop because it will give you an error if you dont.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1