Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2005, 1:19 PM   #1
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
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
and so on (including the last newline)


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)
UnKnown X is offline   Reply With Quote
Old Dec 10th, 2005, 1:41 PM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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
This will go through each line in the file and print it. Simple eh?
Cerulean is offline   Reply With Quote
Old Dec 10th, 2005, 1:45 PM   #3
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
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?
UnKnown X is offline   Reply With Quote
Old Dec 10th, 2005, 1:51 PM   #4
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Dec 10th, 2005, 1:55 PM   #5
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Thanks, I'll try that!
UnKnown X is offline   Reply With Quote
Old Dec 10th, 2005, 1:57 PM   #6
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Dec 10th, 2005, 2:16 PM   #7
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
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
...
UnKnown X is offline   Reply With Quote
Old Dec 10th, 2005, 3:40 PM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by UnKnown X
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
...
I'm not quite sure what you mean. Is the above "code" meant as input from a file, or output from your python program?

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),
To input it, you can use the split function, which divides a string based on a specified delimiting character:
for line in file:
   number, data = line.split(":")
Arevos is offline   Reply With Quote
Old Dec 10th, 2005, 4:14 PM   #9
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
It works perfectly! Thanks!
UnKnown X is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC