Very dumb question

L

Laszlo Zsolt Nagy

I have a program with this code fragment:

print len(data)
print data[:50]
raise SystemExit

This prints:

20381
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

But if I change 50 to 51

print len(data)
print data[:51]
raise SystemExit

then it prints

20381
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

After all, only the last 50 bytes are printed. The string is the same
(length 20381) in both cases.
Surprisingly, I can print more than 50 characters, this works:

print "012345678901234567890123456789012345678901234567890123456789A"

I'm sure it is my mistake, but I don't know what am I doing wrong. Do
you have an idea?
Thanks,

Les
 
B

bruno modulix

Laszlo said:
I have a program with this code fragment:

print len(data)
print data[:50]
raise SystemExit

This prints:

20381
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

But if I change 50 to 51

print len(data)
print data[:51]
raise SystemExit

then it prints

20381
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

After all, only the last 50 bytes are printed. The string is the same
(length 20381) in both cases. (snip

I'm sure it is my mistake, but I don't know what am I doing wrong. Do
you have an idea?

I assume the code snippets are exact copy/paste so this is not a typo
(like print data[51:] ...) - and I can't reproduce it here... even with
a string of 20381 characters.

mmm...

type(data) ???
 
P

Peter Otten

Laszlo said:
I have a program with this code fragment:

print len(data)
print data[:50]
raise SystemExit

This prints:

20381
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

But if I change 50 to 51

print len(data)
print data[:51]
raise SystemExit

then it prints

20381
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

The closest I can get to your situation is

[on linux]
print "abc\rx"[:4] abc
print "abc\rx"[:5]
xbc

i. e. a character after a 'carriage return' ('\r') overwrites part of the
string which therefore doesn't seem to grow. Try

print repr(data[:51])

to see what's really in your data string.

Peter
 
L

Laszlo Zsolt Nagy

I assume the code snippets are exact copy/paste so this is not a typo
(like print data[51:] ...) - and I can't reproduce it here... even with
a string of 20381 characters.
Yes, they were cut out. type(data) returns '<type str>'.
The data was downloaded from a website, it starts with

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

Even if it had special control characters in it, I do not understand the
effect (printing only the first 50 chars).
But probably it is an issue with the terminal, since it is working fine
on Windows.

Les
 
L

Laszlo Zsolt Nagy

i. e. a character after a 'carriage return' ('\r') overwrites part of the
string which therefore doesn't seem to grow. Try

print repr(data[:51])

to see what's really in your data string.
Yes, that was it! Thanks for you help. I thought it will be something
obvious.
The server returned carriage returns in the HTML source code, and I
tried to print that. :)

Les
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top