Here is my code atm

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?

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:

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()

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.

Sorry i should have been more descriptive :)

The Script

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

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

fileObj.write(single_line)

for some reason it always saves the third line of roombookings.txt no matter what the user inputs :S What am i doing wrong here haha

Ok, so I rewrote some stuff and commented it a bit to help you out.

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()

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!

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 :D Thanks again man :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.