[Tutor] String.find won't - why?

Danny Kohn danny.kohn@systematik.se
Mon, 29 Oct 2001 00:04:17 +0100

This is a multi-part message in MIME format.

Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable

Hi again.
Thanks for all the comments on the readline(s) issue. I have understood =
this fully.
But please bear with me. Unfortunally the next step in this little =
program is as problematic for me.
Am trying to remove all 0D 0A (Hex). Thought this was done through the =
following while loop.
The file is attached.
The first round things works nicely. The 0D 0A (Hex) is found and =
removed. Pos is correctly 142.
The second round things behave more strangely. Pos will get a wrong =
value (178) but there just is no 0D 0A (Hex) there and so the i:pos =
string will get to short.
Running w2k. Why on earth is this behaving wrongly?

t =3D open('d:/Experiment.txt', 'r')

text =3D t.read()    # Read text

print text

a =3D  ''
while i < len(text):
	pos =3D string.find(text[i:], '\n')
	print pos
	if pos > 0:
		a =3D a + text[i:pos]
		print a
	i =3D i + pos + 1


Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;

Comments that contradict the code are worse than no comments. Always =
make a priority of keeping the comments up-to-date when the code =
If a comment is a phrase or sentence, its first word should be =
capitalized, unless it is an identifier that begins with a lower case =
letter (never alter the case of identifiers!).

If a comment is short, the period at the end is best omitted. Block =
comments generally consist of one or more paragraphs built out of =
complete sentences, and each sentence should end in a period.

You can use two spaces after a sentence-ending period.

As always when writing English, Strunk and White apply.

Python coders from non-English speaking countries: please write your =
comments in English, unless you are 120% sure that the code will never =
be read by people who don't speak your language.

Block comments generally apply to some (or all) code that follows them, =
and are indented to the same level as that code. Each line of a block =
comment starts with a # and a single space (unless it is indented text =
inside the comment). Paragraphs inside a block comment are separated by =
a line containing a single #. Block comments are best surrounded by a =
blank line above and below them (or two lines above and a single line =
below for a block comment at the start of a a new section of function =