Python not putting out actual result -


i'm compiling list essid of available networks in area used purpose.

this script:

from subprocess import check_output scanoutput = check_output(["iwlist", "wlan0", "scan"])  line in scanoutput.split():         if line.startswith("essid:"):                 line=line[7:-1]                 print line 

when run command "sudo iwlist wlan0 scan | grep essid" ssh, output:

            essid:"easybell dsl"             essid:"fritz!box fon wlan 7360 sl_ext"             essid:"notrespassing"             essid:"wlan-519293"             essid:"cinque"             essid:"easybox-738461"             essid:"fritz!box wlan 3270"             essid:"easybox-a2b246" 

sadly, python script outputs:

easybel fritz!bo notrespassing wlan-519293 cinque easybox-738461 fritz!bo easybox-a2b246 

with closer inspection looks borks out when there space, seems remove 1 last letter before space before printing it. can explain going on here?

as far can tell code

line=line[7:-1] 

is 7 responsible remove first 7 characters, thus: essid:" , -1 responsible last " resulting in clean line ssid name.

when run code without line output:

essid:"easybell essid:"notrespassing" essid:"wlan-519293" essid:"cinque" essid:"easybox-738461" essid:"fritz!box essid:"fritz!box 

how possible ssh line can proper information, when have python run code looses parts?

any way fix this?

thanks!

split() default cuts on whitespace, need explicitly set split() separator \n:

string.split(s[, sep[, maxsplit]]): if optional second argument sep absent or none, words separated arbitrary strings of whitespace characters (space, tab, newline, return, formfeed)

>>> test = ''' essid:"easybell dsl" essid:"fritz!box fon wlan 7360 sl_ext" essid:"notrespassing" [...] ''' >>> l in test.split(): ...     if l.startswith('essid'): ...         print(l[7:-1]) ...          easybel fritz!bo notrespassing [...] >>> l in test.split('\n'): ...     if l.startswith('essid'): ...         print(l[7:-1]) ...          easybell dsl fritz!box fon wlan 7360 sl_ext notrespassing [...] 

or use splitlines():

>>> l in test.splitlines(): ...     if l.startswith('essid'): ...         print(l[7:-1]) ...          easybell dsl fritz!box fon wlan 7360 sl_ext notrespassing [...] 

Comments