[Tutor] manipulating a string in python

Dave Wilder D.Wilder at F5.com
Tue Nov 27 17:49:47 CET 2012


Hello,



I believe there is a simple answer to this, but I am confused on what direction I should go that would perform what  I wish to do most efficiently.



** My Environment ***

[root at f5ite ~/tests]$ python

Python 2.7 (r27:82500, Jul  6 2010, 02:54:50)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2

Type "help", "copyright", "credits" or "license" for more information.



[root at f5ite ~/tests]$ uname -a

Linux VM-QA-ITE-03 2.6.32-220.17.1.el6.i686 #1 SMP Tue May 15 22:09:39 BST 2012 i686 i686 i386 GNU/Linux



[root at f5ite ~/tests]$ more /etc/redhat-release

CentOS release 5.8 (Final)

[root at f5ite ~/tests]$







I need to strip down a string to make a list of "auto" and anything starting w/ "10" and nothing else.



Below is the string I am currently working with.

>> results

'tmsh list net interface 1.1 media-capa \rbilities\nnet interface 1.1 {\n    media-capabilities {\n        none\n        auto\n        10T-FD\n

10T-HD\n        100TX-FD\n        100TX-HD\n        1000T-FD\n        1000T-HD\n    }\n}\n'

>>



I want to chop it up, so I have a list of all media speeds (auto or "10X" that I can then loop through, so would like to end up with something like below:

>> results

'none', 'auto', '10T-FD'...'1000T-HD'



I could do this using a series of "split" commands.  However, is there a better way?  For example, can I just eliminate "media-capabilities", "{" and "}" right off the bat

and then do a split on "\n"?   Should I use "re" matching for this?



I could also just a single split('\n') and then inside the loop ignore everything that is not "auto" or starts with "10", but that seems inefficient.



Any advice on the best way to do this?



Thanks,



Dave


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121127/b107db56/attachment.html>


More information about the Tutor mailing list