[Tutor] Re: [Python-Help] a number and control char stored in a single char. !

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 1 Jun 2001 22:55:28 -0700 (PDT)


On Fri, 1 Jun 2001, kaha aham wrote:


> I am running & capturing screen output of a command using os.popen as
> helped by tuto@python.org ppl. However the value i capture which
> should be a number like something like 22345 is displayed as
> '22345\012'

Yes, this means that what you're getting back is the string "21345"
followed by the newline, '\012'.  What you'll need to do is convert it
with the int() function.



> I need to numerically compare this value later in program. I tried
> slicing it to remove the last control char using var[:-1], on which it
> prints []. I then trid using len(), it tells size as 1 char. I tried
> printing just 1st char using var[0], and it shows '22345\012'

Ah!  You had the right idea: what you'll want to do is:

    var[0][:-1]

instead.  You want to get all but the last character from var[0], your
first line of the output.  This is probably what was preventing you from
converting to integer.  Try int()'ing with var[0][:-1], and you should be
ok.  But if you're converting with int(), you don't need to strip out the
newline character; int() knows how to deal with it already.


Let's look at what happened before... I'm guessing that when you used
os.popen(), you used the readlines() method to get at all the lines of
output.  What this means is that "var" stands for all the lines of input,
and "var[0]" stands for the first line.  However, "var[:-1]" stands for
all but the last line --- but if your output only consists of one line,
that means you'll get the empty list, [].  That should explain the weird
error messages.


> That's baffling me. How can it put all that number and control char in
> 1 char. I currently can't post to tutor maillist as I did earlier.

Hmmm... that's strange!  Try posting your question again; perhaps the mail
host was down.  In the meantime, I'll forward your message to
tutor@python.org.

I just checked, and from what I can tell, you're not subscribed to the
tutor list yet.  You'll probably need to subscribe to post to tutor.  You
can subscribe here:

    http://mail.python.org/mailman/listinfo/tutor


If you run into difficulties again, feel free to email again.  I'll talk
to you later!