Thread: Reading a file
View Single Post
Old 05-01-2006, 07:28 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Perhaps this small sample code will help you along:
Code:
#!/usr/bin/python
import re                      # make regular expression available
list = []
f = open("Studentlist.txt", "r") # open file
lines = f.readlines()          # make every line available
for line in lines:	       # loop through lines
    if not re.match("^-", line): # if line dosn't start with '-'
          list.append(line[0:-1])# Add to line array without '\n'
del list[0]                    # remove the first line "ID | etc "
for item in list:
    print item
FOr reference, as teknomage noted, you might want to read up on the regular expression object in python, I find The Regular expression howto usefull, from my code, what you need to do, is to find a way to split your lines in the list, so you'll end up with a list like:
Code:
[["12400", "JOHN MAN", "IT"],
 ["12500", "NICE JOHN", "LAW"],
 ["12600", "PATRICK MAN", "CS"]]
Right now the list in my code will end up containing
Code:
["12400 JOHN MAN IT",
 "12500 NICE JOHN LAW",
 "12600 PATRICK MAN CS"]
Which can be acomplished by using the regular expression methods on the list created by my small code portion.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is online now   Reply With Quote