943,807 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1557
  • Python RSS
Jul 6th, 2009
0

How to save results from script to a txt file

Expand Post »
Here is my code atm

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jabbadabba is offline Offline
3 posts
since Jul 2009
Jul 6th, 2009
0

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

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 6th, 2009
0

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

Sorry i should have been more descriptive

The Script

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jabbadabba is offline Offline
3 posts
since Jul 2009
Jul 6th, 2009
0

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

Ok, so I rewrote some stuff and commented it a bit to help you out.
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 6th, 2009
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jabbadabba is offline Offline
3 posts
since Jul 2009
Mar 6th, 2010
0
Re: How to save results from script to a txt file
I'm asking for help on this problem, but it is the question before the one that asks for the results to be printed out and has an added ask: to print only two elements of the list ...

so if the list structure is made up of 'Date', 'Room', 'Course', 'Stage'...
the action is to search each line for the 'Room' that the user inputted...
and for each line that contains that 'Room'...
print 'Date', 'Course'

They added a description of split but i get error messages saying tuples and lists cant call ... so here is it simply but without being able to only print the first and third elements only...

>>> ups = open('roombookings.txt', 'r')
>>> fort = raw_input('tyke: ')
tyke: L3
>>> for line in ups:
if fort in line:
print line


6-3-07,L3,WEB201,1

7-3-07,L3,WEB201,2

... so what i'm after doing is printing at the very least

6-3-07,WEB201

7-3-07,WEB201

... and then being able to embellish it to ...

print 'The room bookings found for ' + tyke + ' are ' thesolvedproblem

Thanks in anticipation ... !

Editor's note:
Attaching your question to a solved thread is not a good idea. The thread is solved, so most folks will not look in it for additional questions. This will limit help.

Please post your question in a properly titled thread of you rown.
Last edited by vegaseat; Mar 9th, 2010 at 11:12 am. Reason: don't hijack solve threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rueth is offline Offline
1 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Pyhton question
Next Thread in Python Forum Timeline: how to create variables in each iteration of a loop?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC