[Tutor] Re: [Python-Help] a number and control char stored in
a single char. !
GADGIL PRASAD /INFRA/INFOTECH
GADGILP@INFOTECH.ICICI.com
Mon, 4 Jun 2001 09:17:55 +0530
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C0ECA9.238B6520
Content-Type: text/plain;
charset="iso-8859-1"
hello,
daniel, u r a life saver! Thanks.
well what i meant when i said, i cant post to tutor list, is that, I was
posting
from cybercafe, couldn't change sender's addr to my subscribed
id. I thought non subscribers can't post.
/regards
prasad
> -----Original Message-----
> From: Daniel Yoo [mailto:dyoo@hkn.eecs.berkeley.edu]
> Sent: Saturday, June 02, 2001 11:25 AM
> Cc: help@python.org; tutor@python.org
> Subject: [Tutor] Re: [Python-Help] a number and control char
> stored in a
> single char. !
>
>
> 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!
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
.
------_=_NextPart_001_01C0ECA9.238B6520
Content-Type: text/html;
charset="iso-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [Tutor] Re: [Python-Help] a number and control char stored in a single char. !</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>hello,</FONT>
</P>
<P><FONT SIZE=2>daniel, u r a life saver! Thanks.</FONT>
</P>
<P><FONT SIZE=2>well what i meant when i said, i cant post to tutor list, is that, I was posting </FONT>
<BR><FONT SIZE=2>from cybercafe, couldn't change sender's addr to my subscribed</FONT>
<BR><FONT SIZE=2>id. I thought non subscribers can't post.</FONT>
</P>
<P><FONT SIZE=2>/regards</FONT>
<BR><FONT SIZE=2>prasad</FONT>
</P>
<BR>
<P><FONT SIZE=2>> -----Original Message-----</FONT>
<BR><FONT SIZE=2>> From: Daniel Yoo [<A HREF="mailto:dyoo@hkn.eecs.berkeley.edu">mailto:dyoo@hkn.eecs.berkeley.edu</A>]</FONT>
<BR><FONT SIZE=2>> Sent: Saturday, June 02, 2001 11:25 AM</FONT>
<BR><FONT SIZE=2>> Cc: help@python.org; tutor@python.org</FONT>
<BR><FONT SIZE=2>> Subject: [Tutor] Re: [Python-Help] a number and control char </FONT>
<BR><FONT SIZE=2>> stored in a</FONT>
<BR><FONT SIZE=2>> single char. !</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> On Fri, 1 Jun 2001, kaha aham wrote:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> > I am running & capturing screen output of a command using </FONT>
<BR><FONT SIZE=2>> os.popen as</FONT>
<BR><FONT SIZE=2>> > helped by tuto@python.org ppl. However the value i capture which</FONT>
<BR><FONT SIZE=2>> > should be a number like something like 22345 is displayed as</FONT>
<BR><FONT SIZE=2>> > '22345\012'</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Yes, this means that what you're getting back is the string "21345"</FONT>
<BR><FONT SIZE=2>> followed by the newline, '\012'. What you'll need to do is convert it</FONT>
<BR><FONT SIZE=2>> with the int() function.</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> > I need to numerically compare this value later in program. I tried</FONT>
<BR><FONT SIZE=2>> > slicing it to remove the last control char using var[:-1], </FONT>
<BR><FONT SIZE=2>> on which it</FONT>
<BR><FONT SIZE=2>> > prints []. I then trid using len(), it tells size as 1 char. I tried</FONT>
<BR><FONT SIZE=2>> > printing just 1st char using var[0], and it shows '22345\012'</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Ah! You had the right idea: what you'll want to do is:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> var[0][:-1]</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> instead. You want to get all but the last character from var[0], your</FONT>
<BR><FONT SIZE=2>> first line of the output. This is probably what was </FONT>
<BR><FONT SIZE=2>> preventing you from</FONT>
<BR><FONT SIZE=2>> converting to integer. Try int()'ing with var[0][:-1], and </FONT>
<BR><FONT SIZE=2>> you should be</FONT>
<BR><FONT SIZE=2>> ok. But if you're converting with int(), you don't need to </FONT>
<BR><FONT SIZE=2>> strip out the</FONT>
<BR><FONT SIZE=2>> newline character; int() knows how to deal with it already.</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Let's look at what happened before... I'm guessing that when you used</FONT>
<BR><FONT SIZE=2>> os.popen(), you used the readlines() method to get at all the lines of</FONT>
<BR><FONT SIZE=2>> output. What this means is that "var" stands for all the </FONT>
<BR><FONT SIZE=2>> lines of input,</FONT>
<BR><FONT SIZE=2>> and "var[0]" stands for the first line. However, "var[:-1]" </FONT>
<BR><FONT SIZE=2>> stands for</FONT>
<BR><FONT SIZE=2>> all but the last line --- but if your output only consists of </FONT>
<BR><FONT SIZE=2>> one line,</FONT>
<BR><FONT SIZE=2>> that means you'll get the empty list, []. That should </FONT>
<BR><FONT SIZE=2>> explain the weird</FONT>
<BR><FONT SIZE=2>> error messages.</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> > That's baffling me. How can it put all that number and </FONT>
<BR><FONT SIZE=2>> control char in</FONT>
<BR><FONT SIZE=2>> > 1 char. I currently can't post to tutor maillist as I did earlier.</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> Hmmm... that's strange! Try posting your question again; </FONT>
<BR><FONT SIZE=2>> perhaps the mail</FONT>
<BR><FONT SIZE=2>> host was down. In the meantime, I'll forward your message to</FONT>
<BR><FONT SIZE=2>> tutor@python.org.</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> I just checked, and from what I can tell, you're not subscribed to the</FONT>
<BR><FONT SIZE=2>> tutor list yet. You'll probably need to subscribe to post to </FONT>
<BR><FONT SIZE=2>> tutor. You</FONT>
<BR><FONT SIZE=2>> can subscribe here:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> <A HREF="http://mail.python.org/mailman/listinfo/tutor" TARGET="_blank">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> If you run into difficulties again, feel free to email again. </FONT>
<BR><FONT SIZE=2>> I'll talk</FONT>
<BR><FONT SIZE=2>> to you later!</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> _______________________________________________</FONT>
<BR><FONT SIZE=2>> Tutor maillist - Tutor@python.org</FONT>
<BR><FONT SIZE=2>> <A HREF="http://mail.python.org/mailman/listinfo/tutor" TARGET="_blank">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
<BR><FONT SIZE=2>> </FONT>
</P>
<BR>
<P><B><FONT SIZE=2>.. </FONT></B>
</P>
</BODY>
</HTML>
------_=_NextPart_001_01C0ECA9.238B6520--