[Tutor] search and replace

2006-03-06 Thread tak
Hello,

I have a problem finding specific words.
I would like to filter out a word or replace it in a file.
I notices that the re module is good for finding patterns.

I am trying to filter out a word from strings, but having a little trouble.
I was wondering if I can search a string for a specific word and nothing
else.

For example:
hello, Othello. # just the hello and not Othello

I know that I could probably put the string in a list and replace it.
But I was wondering if it is possible using re module or the other find
type attributes to find it.
hello will not be necessarily at the beginning or end, so I am opting
not to use endswith or startswith.
Thank you.

Best regards,

tak


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to write a string into a specific line in a file

2006-03-07 Thread tak
Hello,

I was wondering if it is possible to write a string to a specific line
in a file
without reading in the whole file in as the below.
___

f = open(filename)
lines = f.readlines()
f.close()
# num for some line number
line[num] = "String"
f = open(filename)
f.writelines(lines)
f.close()


Writing directly to the line number would be ideal.
Some thing like: f.write(line number, string) if there is a function
like that.

Or would the best way to do line replacement be through iteration.
__

for line in open(filename):
# write to current line???

_

Thank you.

Best regards,

Tak


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to write a string into a specific line in a file

2006-03-07 Thread tak
Sorry, I meant lines in line in the below:

f = open(filename)
lines = f.readlines()
f.close()
# num for some line number
>>lines[num] = "String"
f = open(filename)
f.writelines(lines)
f.close()



***

Hello,

I was wondering if it is possible to write a string to a specific line
in a file
without reading in the whole file in as the below.
___

f = open(filename)
lines = f.readlines()
f.close()
# num for some line number
line[num] = "String"
f = open(filename)
f.writelines(lines)
f.close()


Writing directly to the line number would be ideal.
Some thing like: f.write(line number, string) if there is a function
like that.

Or would the best way to do line replacement be through iteration.
__

for line in open(filename):
# write to current line???

_

Thank you.

Best regards,

Tak


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor