Thread: Reading a file
View Single Post
Old 05-04-2006, 08:12 AM   #15 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
It's the indentation, where otehr programming languagess wants scopes defining where what starts/ends by using brackets, python uses indentation, in your example code you have the for loop printing the item from returnline within the scope of you for loop reading from the file, in C/C++ it would be equivalent to writing something like this:
Code:
int j=0, i;
while(fgets(buffer, MAX_SIZE, file_pointer))
{
    buffer[strlen(buffer)-1] = '\0';
    strcpy(list[j], buffer);
    for(i=0; i< j; ++i)
    {
        printf("%s\n", list[i]);
    }
    ++j;
}
As you can see the printing of your gathered lines should only occure when you've finished filling in the list of lines found in the file, so obvius we're looking for this solution:
Code:
int j=0, i;
while(fgets(buffer, MAX_SIZE, file_pointer))
{
    buffer[strlen(buffer)-1] = '\0';
    strcpy(list[j], buffer);
    ++j;
}
for(i=0; i< j; ++i)
{
    printf("%s\n", list[i]);
}
This should work, but not testet:
Code:
import re
returnline = []
f = open("List.txt", "r")
for line in f.readlines():
    returnline.append(re.compile(' ').sub(',', line[0:-1]))
for item in returnline:
    print item
Note the way theres no indentation befor the second for line.
__________________
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