![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Reading numbers from file
I read ColdDeath's (brilliant
) tutorial, and decided to give Python a shot. So I made a programme that finds a Fibonacci number a few hours ago. Then I decided to come back and try to make it read from and write the results to a file.Now, I can't figure out how to make it read the last number! The numbers are saved like this: 1 1 2 3 5 8 13 21 34 55 I tried this, but it didn't work. I'm not sure where or why it fails: k = 1
while (f.seek(k * -1, 2)) != '\n':
k += 1
k -= 1
f.seek((f.read())-(f.read()-k))
b = int(f.read((f.read())-(f.read()-k)))
k += 2
while (f.seek(k * -1, 2)) != '\n':
k += 1
k -= 1
f.seek((f.read())-(f.read()-k))
a = int(f.read((f.read())-(f.read()-k)))
f.seek(0, 2) |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
I can't see any reason why you're doing all of this manual seeking and reading - this isn't C. Python lets you iterate over the lines in a file really easily:
myFile = file("fibnumbers.txt")
for line in myFile:
print line |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Well, C++ is the only other language I know :p
But how can I make it stop at the last line and save that to a variable, then the second last line to another variable? |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Shove each line into a list and then access the last and second last elements in this list.
lines = []
for line in myFile:
lines.append(line)
lastItem = lines[-1] # -1 refers to the last item in a list
secondLastItem = lines[-2] # -2 refers to the second last item in a list.. etc. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Thanks, I'll try that!
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
Unknown X, as Cerulean said, this is python not C.
Python has a completely different mindset to languages such as C. I suggest reading "Dive into Python" (you can buy the book or read it for free at diveintopython.org)
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
Thanks for the link, coldDeath! I'll have a look now.
Yet another question: How would I make it write the line number next to the fibonacci number, and then ignore it (the line number)? I.e: 1: 1 2: 1 3: 2 4: 3 5: 5 6: 8 7: 13 8: 21 9: 34 ... |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
To output it, you can use the enumerate function, which encodes a running count into any sequence: for number, line in enumerate(file): print "%d: %s" % (number, line), for line in file:
number, data = line.split(":") |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
It works perfectly! Thanks!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|