Thread: Reading a file
View Single Post
Old 05-02-2006, 12:24 PM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
That is not how split() works, it splits the line up by the given parameter, the way you are using it, it would split the lines where ever it encounters a ',', but you want to replace every space with your ',', that way you would need something like this:
Code:
f = open("List", "r") #open and read file
returnline = [] #empty list
for line in f.readlines():
    returnline.append(re.compile(' ').sub(',', line[0:-1]))
as an explanation, the
Code:
re.compile(' ')
will set it to be active at every space found, the
Code:
.subn(',', line[0:-1])
will substitute every occurence of the activated part (the space) within the line[0:-1] (meaning the line without the trailing '\n') with the comma (',')
Since the line can be accessed as a char array, theres no point in trying to replace '\n' with anything, since it will allways remain as the very last char (index END-1, thus [0:-1] meaning char array starting from index 0, first char to position -1 from end) beacause when reading from a file, you will allways read to EOF or EOL (end of line) hence where '\n' resides.

Then later on, you can use split() to split at every ',' in your line, if that is what you wishes, but then again, I can't see why it wouldn't be usefull with my usage, where I split it at every space.. It would be the same result, only a few more operations in your solution.
__________________
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

Last edited by redhead; 05-02-2006 at 12:43 PM.
redhead is offline   Reply With Quote