[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>&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt; From: Daniel Yoo [<A HREF="mailto:dyoo@hkn.eecs.berkeley.edu">mailto:dyoo@hkn.eecs.berkeley.edu</A>]</FONT>
<BR><FONT SIZE=2>&gt; Sent: Saturday, June 02, 2001 11:25 AM</FONT>
<BR><FONT SIZE=2>&gt; Cc: help@python.org; tutor@python.org</FONT>
<BR><FONT SIZE=2>&gt; Subject: [Tutor] Re: [Python-Help] a number and control char </FONT>
<BR><FONT SIZE=2>&gt; stored in a</FONT>
<BR><FONT SIZE=2>&gt; single char. !</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; On Fri, 1 Jun 2001, kaha aham wrote:</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; &gt; I am running &amp; capturing screen output of a command using </FONT>
<BR><FONT SIZE=2>&gt; os.popen as</FONT>
<BR><FONT SIZE=2>&gt; &gt; helped by tuto@python.org ppl. However the value i capture which</FONT>
<BR><FONT SIZE=2>&gt; &gt; should be a number like something like 22345 is displayed as</FONT>
<BR><FONT SIZE=2>&gt; &gt; '22345\012'</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Yes, this means that what you're getting back is the string &quot;21345&quot;</FONT>
<BR><FONT SIZE=2>&gt; followed by the newline, '\012'.&nbsp; What you'll need to do is convert it</FONT>
<BR><FONT SIZE=2>&gt; with the int() function.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; &gt; I need to numerically compare this value later in program. I tried</FONT>
<BR><FONT SIZE=2>&gt; &gt; slicing it to remove the last control char using var[:-1], </FONT>
<BR><FONT SIZE=2>&gt; on which it</FONT>
<BR><FONT SIZE=2>&gt; &gt; prints []. I then trid using len(), it tells size as 1 char. I tried</FONT>
<BR><FONT SIZE=2>&gt; &gt; printing just 1st char using var[0], and it shows '22345\012'</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Ah!&nbsp; You had the right idea: what you'll want to do is:</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; var[0][:-1]</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; instead.&nbsp; You want to get all but the last character from var[0], your</FONT>
<BR><FONT SIZE=2>&gt; first line of the output.&nbsp; This is probably what was </FONT>
<BR><FONT SIZE=2>&gt; preventing you from</FONT>
<BR><FONT SIZE=2>&gt; converting to integer.&nbsp; Try int()'ing with var[0][:-1], and </FONT>
<BR><FONT SIZE=2>&gt; you should be</FONT>
<BR><FONT SIZE=2>&gt; ok.&nbsp; But if you're converting with int(), you don't need to </FONT>
<BR><FONT SIZE=2>&gt; strip out the</FONT>
<BR><FONT SIZE=2>&gt; newline character; int() knows how to deal with it already.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Let's look at what happened before... I'm guessing that when you used</FONT>
<BR><FONT SIZE=2>&gt; os.popen(), you used the readlines() method to get at all the lines of</FONT>
<BR><FONT SIZE=2>&gt; output.&nbsp; What this means is that &quot;var&quot; stands for all the </FONT>
<BR><FONT SIZE=2>&gt; lines of input,</FONT>
<BR><FONT SIZE=2>&gt; and &quot;var[0]&quot; stands for the first line.&nbsp; However, &quot;var[:-1]&quot; </FONT>
<BR><FONT SIZE=2>&gt; stands for</FONT>
<BR><FONT SIZE=2>&gt; all but the last line --- but if your output only consists of </FONT>
<BR><FONT SIZE=2>&gt; one line,</FONT>
<BR><FONT SIZE=2>&gt; that means you'll get the empty list, [].&nbsp; That should </FONT>
<BR><FONT SIZE=2>&gt; explain the weird</FONT>
<BR><FONT SIZE=2>&gt; error messages.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; &gt; That's baffling me. How can it put all that number and </FONT>
<BR><FONT SIZE=2>&gt; control char in</FONT>
<BR><FONT SIZE=2>&gt; &gt; 1 char. I currently can't post to tutor maillist as I did earlier.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; Hmmm... that's strange!&nbsp; Try posting your question again; </FONT>
<BR><FONT SIZE=2>&gt; perhaps the mail</FONT>
<BR><FONT SIZE=2>&gt; host was down.&nbsp; In the meantime, I'll forward your message to</FONT>
<BR><FONT SIZE=2>&gt; tutor@python.org.</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; I just checked, and from what I can tell, you're not subscribed to the</FONT>
<BR><FONT SIZE=2>&gt; tutor list yet.&nbsp; You'll probably need to subscribe to post to </FONT>
<BR><FONT SIZE=2>&gt; tutor.&nbsp; You</FONT>
<BR><FONT SIZE=2>&gt; can subscribe here:</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; <A HREF="http://mail.python.org/mailman/listinfo/tutor" TARGET="_blank">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; If you run into difficulties again, feel free to email again. </FONT>
<BR><FONT SIZE=2>&gt;&nbsp; I'll talk</FONT>
<BR><FONT SIZE=2>&gt; to you later!</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; _______________________________________________</FONT>
<BR><FONT SIZE=2>&gt; Tutor maillist&nbsp; -&nbsp; Tutor@python.org</FONT>
<BR><FONT SIZE=2>&gt; <A HREF="http://mail.python.org/mailman/listinfo/tutor" TARGET="_blank">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
<BR><FONT SIZE=2>&gt; </FONT>
</P>
<BR>

<P><B><FONT SIZE=2>.. </FONT></B>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C0ECA9.238B6520--