[Tutor] Extract from file
Monte Milanuk
memilanuk at gmail.com
Wed Jun 9 05:19:32 CEST 2010
On 6/8/10 5:44 PM, Manju wrote:
> Hi,
>
> I need help with extracting information from file.
>
> I have a file which is a comma delimited text file containing separate
> line for each booking. Each line is composed of date, room number,
> course number and course day.
Course day??? Not sure what you meant by that, on top of the already
mentioned 'date'...?
>
> I need to extract only the room number which is entered at the prompt
> and display course number, date and room number.
>
> I have so far managed accept the entry from the prompt, to open the file
> and display all the text
>
This may be a case of the blind leading the blind, but here goes:
Given this data file (comments not included in actual file):
#
# example.txt
#
# date, room, course
2010-06-08,123,246
2009-06-08,234,468
2008-06-08,345,680
#
# example.py
#
room = raw_input("Enter the room #: ")
file = open("example.txt", "r")
for line in file:
line = line.strip()
line = line.split(',')
if line[1] == room:
print "Date: " + line[0]
print "Room: " + line[1]
print "Course: " + line[2]
file.close()
>>>
Enter the room #: 234
Date: 2009-06-08
Room: 234
Course: 468
>>>
HTH,
Monte
More information about the Tutor
mailing list