| | |
How to save results from script to a txt file
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
Here is my code atm
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?
Python Syntax (Toggle Plain Text)
room_num = raw_input("Enter your room number: ") text_file = open("c:/roombookings.txt", "r") whole_thing = text_file.readline() if room_num in whole_thing: print "Room number found here is the information:" else : print "Room number not found." single_line = whole_thing.split('\n') for single_line in text_file.readlines(): if room_num in single_line: print single_line fileObj = open("courseinfo.dat","w") fileObj.write(???????????????????????????) fileObj.close() 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.
First, your whole_thing variable should be
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:
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.
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)
single_line = whole_thing.split('\n') for single_line in text_file.readlines(): if room_num in single_line: fileObj = open("courseinfo.dat","w") fileObj.write(single_line) fileObj.close()
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
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
Sorry i should have been more descriptive 
The Script
roombookings.txt
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 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

The Script
Python Syntax (Toggle Plain Text)
room_num = raw_input("Enter your room number: ") text_file = open("c:/roombookings.txt", "r") whole_thing = text_file.readline() if room_num in whole_thing: print "Room number found here is the information:" else : print "Room number not found." single_line = whole_thing.split('\n') for single_line in text_file.readlines(): if room_num in single_line: print single_line fileObj = open("courseinfo.dat","w") fileObj.write() fileObj.close() text_file.close()
roombookings.txt
Python Syntax (Toggle Plain Text)
Date, Room, Course, Stage 6-3-07,L1,MSW001,1 6-3-07,L2,MSP201,1 6-3-07,L3,WEB201,1 6-3-07,L4,WEB101,1 6-3-07,L5,WEB101,1 7-3-07,L1,MSW001,2 7-3-07,L2,MSP201,2 7-3-07,L3,WEB201,2 7-3-07,L4,WEB101,2 7-3-07,L5,WEB101,2 8-3-07,L1,WEB101,1 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)
fileObj.write(single_line)
What am i doing wrong here haha Ok, so I rewrote some stuff and commented it a bit to help you out.
The comments explain the changes I made pretty well. You had a few issues like that
python Syntax (Toggle Plain Text)
room_num = raw_input("Enter your room number: ") text_file = open("roombookings.txt", "r") # store a list containing the lines from the file whole_thing = text_file.readlines() # join all the lines into a string to search if room_num in "".join(whole_thing): print "Room number found here is the information:" else: print "Room number not found." # we keep a list of lines that we want to save lines_to_save = [] # cycle each line in our whole_thing list for single_line in whole_thing: if room_num in single_line: print single_line # add the line to the saving list lines_to_save.append(single_line) # here we open the output file fileObj = open("courseinfo.dat","w") # and join our saved lines into one string to write fileObj.write("".join(lines_to_save)) fileObj.close() text_file.close()
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
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- Want to save and load info to and from a txt file thats in a listbox (C++)
- Reading in a .txt file, splitting it and then outputting the data to a listbox (C#)
- Encrypting a .txt file through VB6 (Visual Basic 4 / 5 / 6)
- Send form results to a .txt file (PHP)
- Open, search, replace data, save, close .txt file (VB.NET)
- saving results to a txt file. (Java)
- how do i save a .txt file from my programe? (C)
- How to Script Split for my txt file (VB.NET)
Other Threads in the Python Forum
- Previous Thread: File Handling
- Next Thread: PyQt call widget
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change command convert count csv cturtle cursor def dictionary digital dynamic dynamically enter event examples external file float format frange function google gui hints homework i/o iframe import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session signal simple skinning sprite string strings syntax terminal text threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape whileloop wxpython





Thanks again man 