popen - reading strings - constructing a list from the strings

norseman norseman at hughes.net
Wed May 20 18:40:41 CEST 2009


MRAB wrote:
> Aytekin Vargun wrote:
>> <mailto:python-list at python.org>
>> Hello everybody,
>> I have a question about the way I use os.popen. I am open to other 
>> alternative suggestions like using subprocess or communicate.
>>
>> I have an executable (say read_cell_types.exe) that produces string 
>> outputs. For example, after one execution I got the following outputs 
>> in one line:
>>
>> Neurons Microglia Astrocytes
>>
>> In another execution, there might be different numbers of strings.
>>
>> What I would like to do is to get these strings after running 
>> read_cell_types.exe from my python program dynamically  and construct 
>> radio buttons from the list of strings. In order to do this I need to 
>> construct a list of strings first as in
>>
>> ["Neurons" "Microglia" "Astrocytes"]
>>
>> Then it is easy to construct the radio buttons.
>>
>> When I use the following code
>>
>> command = "read_cell_types " + fileName2           child = 
>> os.popen(command)
>> data = child.read()
>> allObjects=data
>>
>> allObjects (or data) contains the letters of all string words. I think 
>> it is probably like ["N" "e" "u" "r"  ...]
>>
>> How can I construct ["Neurons" "Microglia" "Astrocytes"] instead?
>>
>> I will really appreciate your help.
>> Thanks a lot.
>>
>> PS: fileName2 is a parameter  that I am passing to read_cell_types.exe
>>
> If the words in the string are separated by whitespace then use
> allObjects.split().
=================================================
 From actual code:
Python 2.5.2 with Tkinker supplied with download
Linux Slackware 10.2
Same files tested with identical results on Windows XP Pro
   (the .bin was changed to use the .exe compile on Window$ :)


import os

Scenario 1
               required    optional     optional
               .exe file   token(s)     redirected output
xx= os.popen("shp2dxf.bin al963424.shp >al963424.dxf").readlines()

Then use the output file any way you want.
                ----------

Scenerio 2
xx= os.popen("shp2dxf.bin al963424.shp").readlines()

for i in xx:
   print "\t"+i[:-1]


change the print to the code to process your strings (0r don't and use
it to show progress and just add code to process your strings.)

                ----------

The processed strings can be formatted into a list like below.  The
code (left side) can be left blank and you can fill it in manually or
numbers can be used or whatever you like. In this example, the right
side is what I want to be on the button(s).
          ("string", "same_string") is OK too.

   AttTyp = [
     ("S", "Single Use"),
     ("I", "Intercropping"),
     ("D", "Double Cropping"),
     ("T", "Triple Cropping"),
     ("M", "Mixed Land Use"),
     ("-","---------------"),
     ("t", "Toggle Table Display"),
   ]


Next, this will read the above list and generate the actual buttons if
you are using Tkinter.

   AttB= []
   for mode, text in AttTyp:
     c= Radiobutton(frameAtt, text=text, indicatoron=0,
          variable=AttVal, value=mode, command=getAttTyp)
     AttB.append(c)
     c.pack()
   AttB[5].config(state = DISABLED)  #grays out button
   AttB[6].config(state = DISABLED)  #(state= NORMAL) returns to live


Using the append() allows button control from other parts of the
program. It is not required for the buttons to work, but good to have.
*variable* is what is passed to python. AttVal in the example.
Python reads it by using AttVal.get(). As in if AttVal.get() == ??..
or perhaps  local_var_in_function= AttVal.get().
If you reverse the variables mode and text in the 'for mode...' line
then you need to reverse the two strings on each line of the feed list.
(Or exchange their places in the assignment section :)

Using mode and text as the vars, text goes on the button for the human
to read and mode is the internal value to test to determine which
button got pressed. The names mode and text are arbitrary. Name them as
you wish but place each appropriately in the assignment section.


Happy coding.

Today is 20090520
Steve





More information about the Python-list mailing list