How to save results from script to a txt file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 3
Reputation: jabbadabba is an unknown quantity at this point 
Solved Threads: 0
jabbadabba jabbadabba is offline Offline
Newbie Poster

How to save results from script to a txt file

 
0
  #1
Jul 6th, 2009
Here is my code atm

  1. room_num = raw_input("Enter your room number: ")
  2. text_file = open("c:/roombookings.txt", "r")
  3. whole_thing = text_file.readline()
  4.  
  5. if room_num in whole_thing:
  6. print "Room number found here is the information:"
  7. else :
  8. print "Room number not found."
  9.  
  10. single_line = whole_thing.split('\n')
  11. for single_line in text_file.readlines():
  12. if room_num in single_line:
  13. print single_line
  14.  
  15. fileObj = open("courseinfo.dat","w")
  16. fileObj.write(???????????????????????????)
  17. fileObj.close()
  18.  
  19.  
  20. text_file.close()

The user types in a roomnumber and the script displays the information regarding the roomnumber only. What i need to do is have the script save the same data to a seperate file. I can get the script to create the file but not the results from the search.

I am 85% sure that where i have the ????????? is where the answer lies... however i am not smart enough to know exactly what i am doing wrong

What should i be doing that i have forgotten?
Last edited by jabbadabba; Jul 6th, 2009 at 2:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: How to save results from script to a txt file

 
0
  #2
Jul 6th, 2009
First, your whole_thing variable should be text_file.read() if you want to return all the contents as a string. Or text_file.readlines() if you want to return all the contents as a list of each line. Change this and see if that fixes any problems with your code.

I don't fully understand what you mean by "search results", but I assume you mean the single_line variable if the room number is in that line. If you want to write that line to the "courseinfo.dat" file, the just stick that writing segment inside where you are currently printing the single_line. Like this:
  1. single_line = whole_thing.split('\n')
  2. for single_line in text_file.readlines():
  3. if room_num in single_line:
  4. fileObj = open("courseinfo.dat","w")
  5. fileObj.write(single_line)
  6. fileObj.close()
If this isn't what you meant, please rephrase your question clearly and maybe include some sort of example of what "roombookings.txt" contains, etc.
Last edited by shadwickman; Jul 6th, 2009 at 3:18 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: jabbadabba is an unknown quantity at this point 
Solved Threads: 0
jabbadabba jabbadabba is offline Offline
Newbie Poster

Re: How to save results from script to a txt file

 
0
  #3
Jul 6th, 2009
Sorry i should have been more descriptive

The Script

  1. room_num = raw_input("Enter your room number: ")
  2. text_file = open("c:/roombookings.txt", "r")
  3. whole_thing = text_file.readline()
  4.  
  5. if room_num in whole_thing:
  6. print "Room number found here is the information:"
  7. else :
  8. print "Room number not found."
  9.  
  10. single_line = whole_thing.split('\n')
  11. for single_line in text_file.readlines():
  12. if room_num in single_line:
  13. print single_line
  14.  
  15. fileObj = open("courseinfo.dat","w")
  16. fileObj.write()
  17. fileObj.close()
  18.  
  19.  
  20. text_file.close()


roombookings.txt

  1. Date, Room, Course, Stage
  2. 6-3-07,L1,MSW001,1
  3. 6-3-07,L2,MSP201,1
  4. 6-3-07,L3,WEB201,1
  5. 6-3-07,L4,WEB101,1
  6. 6-3-07,L5,WEB101,1
  7. 7-3-07,L1,MSW001,2
  8. 7-3-07,L2,MSP201,2
  9. 7-3-07,L3,WEB201,2
  10. 7-3-07,L4,WEB101,2
  11. 7-3-07,L5,WEB101,2
  12. 8-3-07,L1,WEB101,1
  13. 8-3-07,L2,MSP201,3

What i need to do is create a script that prompts the user for a room number(L1,L2 etc), then it should display each line relating to the room number the user entered.

I have managed to do this, however now i need to get it to save those results to a file called courseinfo.dat.

If i type in
  1. fileObj.write(single_line)
for some reason it always saves the third line of roombookings.txt no matter what the user inputs What am i doing wrong here haha
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: How to save results from script to a txt file

 
0
  #4
Jul 6th, 2009
Ok, so I rewrote some stuff and commented it a bit to help you out.
  1. room_num = raw_input("Enter your room number: ")
  2. text_file = open("roombookings.txt", "r")
  3. # store a list containing the lines from the file
  4. whole_thing = text_file.readlines()
  5.  
  6. # join all the lines into a string to search
  7. if room_num in "".join(whole_thing):
  8. print "Room number found here is the information:"
  9. else:
  10. print "Room number not found."
  11.  
  12. # we keep a list of lines that we want to save
  13. lines_to_save = []
  14.  
  15. # cycle each line in our whole_thing list
  16. for single_line in whole_thing:
  17. if room_num in single_line:
  18. print single_line
  19. # add the line to the saving list
  20. lines_to_save.append(single_line)
  21.  
  22. # here we open the output file
  23. fileObj = open("courseinfo.dat","w")
  24. # and join our saved lines into one string to write
  25. fileObj.write("".join(lines_to_save))
  26. fileObj.close()
  27.  
  28. text_file.close()
The comments explain the changes I made pretty well. You had a few issues like that readline() that I explained. You were on the right track though! Remember that the file write(str) function will overwrite all the file's contents with the string passed to it, so if you were to call it during the for loop, it would overwrite everything else it had saved before. Hence saving the lines to a list to write at the end. You could also create the file before-hand and then open it again in append mode, but this works fine for your problem. I hope the comments explain the code properly. If not, just ask me to clarify!
Last edited by shadwickman; Jul 6th, 2009 at 4:02 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: jabbadabba is an unknown quantity at this point 
Solved Threads: 0
jabbadabba jabbadabba is offline Offline
Newbie Poster

Re: How to save results from script to a txt file

 
0
  #5
Jul 6th, 2009
Thank you
THANK you
THANK YOU



Haha if you haven't noticed shadwickman has shown me what i have done wrong and the script works perfectly Thanks again man
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 580 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC