Reading/Writing from *.txt files in python

Exactly how do you read from file - and convert to real

Page 1 of 1

7 Replies - 1990 Views - Last Post: 24 February 2010 - 05:06 PM Rate Topic: -----

#1 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

Reading/Writing from *.txt files in python

Posted 23 February 2010 - 04:30 PM

Read the following file: (Which has 123.45 in line 1 - and thats it)
myfile = open("numbers.txt")
numbers = myfile.read(10)
myfile.close()
TheNumber = numbers

#Then add TheNumber to a realnumber like; 200.01
It bombs,

Why?

I think that the .read reads a string. But fine, how do you either read a real, or convert the string to real?
Is This A Good Question/Topic? 0
  • +

Replies To: Reading/Writing from *.txt files in python

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Reading/Writing from *.txt files in python

Posted 23 February 2010 - 04:33 PM

Can you post all the code to your program so that we can see exactly where the error might be?
Was This Post Helpful? 0
  • +
  • -

#3 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

Re: Reading/Writing from *.txt files in python

Posted 23 February 2010 - 04:39 PM

View PostDark_Nexus, on 23 February 2010 - 03:33 PM, said:

Can you post all the code to your program so that we can see exactly where the error might be?


here is the code:
def main():
endProg = 'n'
select = 0
myfile = open("budget.txt")
budget = myfile.read(10)
myfile.close()
totalBudget = budget
print "Your budget from your last session was: $", totalBudget
session = raw_input('Would you like to continue from your last session? (Enter y or n):')
if session == "y" or session == "Y":
endProg = "n"
else:
totalBudget = input('Enter your estimated take home pay for this month: $')
while endProg == 'n':
choice = 'y'
while choice == 'y':
print '-------------------------------------------------------------------'
print 'Enter 1 to ADD an Expense'
print 'Enter 2 to Remove an Expense'
print 'Enter 3 to ADD Revenue'
print 'Enter 4 to Remove Revenue'
print 'Enter 5 to EXIT'
select = input('Enter your selection: ')
if select == 1:
totalBudget = addExpense(totalBudget)
elif select == 2:
totalBudget = removeExpense(totalBudget)
elif select == 3:
totalBudget = addRevenue(totalBudget)
elif select == 4:
totalBudget = removeRevenue(totalBudget)
elif select == 5:
endProg = raw_input('Do you want to end the program? (y or n): ')
if endProg == 'y' or 'Y':
print "Your budget is now $", totalBudget
print 'Program End'
print '-------------------------------------------------------------------'
choice = 'n'
myfile = open("budget.txt")
#not sure what to put here?? any sugestions??
myfile.close()
else:
choice = 'y'
print 'Invalid selection'
else:
print 'Invalid selection'
def addExpense(totalBudget):
plusExpense = input('Enter your expense:$ ')
expenseFreq = input('How many times per month is this expense incurred?: ')
totalBudget = totalBudget - (plusExpense * expenseFreq)
print "Your available funds are: $", totalBudget
if totalBudget < 0:
print 'Your expenses are over your budget! This expense has been rejected.'
totalBudget = totalBudget + (plusExpense * expenseFreq)
print "Your current budget amount is: $", totalBudget
return totalBudget

def removeExpense(totalBudget):
minusExpense = input('Enter the expense to be removed:$ ')
minusExpenseFreq = input('How many times per month was this expense incurred?: ')
totalBudget = totalBudget + (minusExpense * minusExpenseFreq)
print "Your available funds are: $", totalBudget
return totalBudget

def addRevenue(totalBudget):
plusRevenue = input('Enter the amount of revenue to add:$ ')
totalBudget = totalBudget + plusRevenue
print "Your available funds are: $", totalBudget
return totalBudget

def removeRevenue(totalBudget):
minusRevenue = input('Enter the amount of revenue to remove:$ ')
totalBudget = totalBudget - minusRevenue
print "Your available funds are: $", totalBudget
if totalBudget < 0:
print 'You greedy bastard! You want to withdrawl all your funds!'
totalBudget = totalBudget + minusRevenue
print "The limit is: $", totalBudget
print "This withdrawl has been rejected; your available funds are $", totalBudget
return totalBudget
main()
Was This Post Helpful? 0
  • +
  • -

#4 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Reading/Writing from *.txt files in python

Posted 23 February 2010 - 04:45 PM

print 'Program End'
print '-------------------------------------------------------------------'
choice = 'n'
myfile = open("budget.txt")
#not sure what to put here?? any sugestions??
myfile.close()



I'm guessing at that point you want to use:

myfile.write(totalBudget)



What line does the program crash on? The interpreter should give you a stack trace once it crashes.

This post has been edited by Dark_Nexus: 23 February 2010 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

Re: Reading/Writing from *.txt files in python

Posted 23 February 2010 - 04:55 PM

No- thats not what im having problems with. Its at the beginning of the program. Inside Main.
Reading the file works, but bombs because its a string - I think. Can it be converted back to a real or read as a real?
Here is the code again:

def main():
endProg = 'n'
select = 0
myfile = open("budget.txt")
budget = myfile.read(10)
myfile.close()
totalBudget = budget
#This is not code below
Say we want to add to totalBudget.
Like totalBudget = 200.00 + totalBudget
Then I get an error.
Was This Post Helpful? 0
  • +
  • -

#6 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

Re: Reading/Writing from *.txt files in python

Posted 23 February 2010 - 05:08 PM

FYI -
If I take out the code for reading the file. And add :
totalBudget = 4000.00

The program works!!
Was This Post Helpful? 0
  • +
  • -

#7 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Reading/Writing from *.txt files in python

Posted 23 February 2010 - 05:11 PM

Here is the documentation to the built-in float function which converts strings to floating point objects:

http://docs.python.o...ght=float#float
Was This Post Helpful? 0
  • +
  • -

#8 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Reading/Writing from *.txt files in python

Posted 24 February 2010 - 05:06 PM

This thread is way too long for such a simple question with a simple answer. No offense , but you need to try reading the question.

budget = float(myfile.read(10))


Although since float() can throw an error if the input is not a number, such as "sausages!", you should surround this with a try..except block.
try:
    budget = float(myfile.read(10))
except ValueError:
    print "Error!"
    return


Was This Post Helpful? -1
  • +
  • -

Page 1 of 1