In the course of playing around with file input and output, I came
across some behavior that is not quite intuitive. I created a simple
text file, test.txt, which contains only 3 lines, and which I expect
will have 5 characters (the digits 1, 2, and 3, and two newline
characters, the first after 1 and the second after 2). Here it is in
all its glory:
1
2
3
However, when I read it using open()and then view it using[color=blue][color=green][color=darkred]
>>> file.seek(0); file.read(); file.tell()[/color][/color][/color]
I get:
'1\n2\n3'
7L
Python thinks there are 7 characters in the file! If I type[color=blue][color=green][color=darkred]
>>> file.seek(1); file.read() OR >>>file.seek(2) ; file.read()[/color][/color][/color]
I get
'\n2\n3'
but[color=blue][color=green][color=darkred]
>>> file.seek(3); file.read()[/color][/color][/color]
gives me what I expected to get with file.seek(2); file.read()
'2\n3'
It appears that Python sometimes counts each of the newline escape
sequences as 2 separate characters and at other times as 1 indivisible
character. What is the appropriate way to think about these
characters?
Thomas Philips
across some behavior that is not quite intuitive. I created a simple
text file, test.txt, which contains only 3 lines, and which I expect
will have 5 characters (the digits 1, 2, and 3, and two newline
characters, the first after 1 and the second after 2). Here it is in
all its glory:
1
2
3
However, when I read it using open()and then view it using[color=blue][color=green][color=darkred]
>>> file.seek(0); file.read(); file.tell()[/color][/color][/color]
I get:
'1\n2\n3'
7L
Python thinks there are 7 characters in the file! If I type[color=blue][color=green][color=darkred]
>>> file.seek(1); file.read() OR >>>file.seek(2) ; file.read()[/color][/color][/color]
I get
'\n2\n3'
but[color=blue][color=green][color=darkred]
>>> file.seek(3); file.read()[/color][/color][/color]
gives me what I expected to get with file.seek(2); file.read()
'2\n3'
It appears that Python sometimes counts each of the newline escape
sequences as 2 separate characters and at other times as 1 indivisible
character. What is the appropriate way to think about these
characters?
Thomas Philips
Comment