stream to string-question

Aldo Cortesi aldo at nullcube.com
Mon Jul 1 04:08:42 EDT 2002


Thus spake Klaus Reinhardt (K.Rdt at TU-Berlin.DE):

> ---------------------------------------------------------------------
> Hi
> 	y=os.popen( 'netstat -a -n','r').read()
> # Aktive Verbindungen
> # 
> #   Proto  Lokale Adresse         Remote-Adresse            Status
> #   TCP    0.0.0.0:1171           0.0.0.0:0              LISTENING
> #   TCP    130.149.164.212:1171   130.149.4.11:110       ESTABLISHED
> 	print y
> 	for i in y: 
> 		print "--------: ", i
> The last is outputting each character in a single line. Is
> there a function to convert this stream in the text-rows?


The variable "y" above is a single, long string with some
embedded newlines. When you iterate over a string you get
its component characters one by one. 

What you really want to do is to split the string up into a
list of lines. Luckily, the string type has a method that
does exactly that... Try:
	
	lines = y.splitlines()
	for l in lines:
		print "---: ", i



Cheers,



Aldo



-- 
Aldo Cortesi
aldo at nullcube.com





More information about the Python-list mailing list