Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to extract columns like awk $1 $5

Reply
Thread Tools

Re: how to extract columns like awk $1 $5

 
 
Craig Ringer
Guest
Posts: n/a
 
      01-07-2005
On Sat, 2005-01-08 at 01:15, Anand S Bisen wrote:
> Hi
>
> Is there a simple way to extract words speerated by a space in python
> the way i do it in awk '{print $4 $5}' . I am sure there should be some
> but i dont know it.


The 'str.split' method is probably what you want:

..>>> x = "The confused frog mumbled something about foxes"
..>>> x.split()
['The', 'confused', 'frog', 'mumbled', 'something', 'about', 'foxes']
..>>> x.split(" ")[4:6]
['something', 'about']

so if 'x' is your string, the rough equivalent of that awk statement is:

..>>> x_words = x.split()
..>>> print x_words[4], x_words[5]

or perhaps

..>>> print "%s %s" % tuple(x.split()[4:6])

--
Craig Ringer

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Matching block of text awk-like: /---/,/---/ A. Farber Perl Misc 9 06-03-2009 04:23 PM
how to extract columns like awk $1 $5 Anand S Bisen Python 7 01-08-2005 05:48 AM
using perl on the command line, like sed or awk gorda Perl Misc 3 10-21-2003 01:50 PM
using perl on the command line, like sed or awk gorda Perl 2 10-21-2003 06:38 AM



Advertisments