Question

I'm looking for a way in python to achieve similar functionality to the unix cut utility. I know I can make a system call and process my data that way but I'd like to make it a bit more "pythonic" and do it with python libraries.

Example text

abcde:12345

I'd like to delimit on : and keep the second field:

cut -d':' -f2

to produce:

12345

thoughts?

Was it helpful?

Solution

You can do:

string.split(":")[1]

where string is your text

OTHER TIPS

Try this:

'abcde:12345'.split(':')[1]

Sure:

for line in open('data.txt'):
    second_field = line.rstrip('\n').split(':')[1]

You can make it more configurable and even write your own with optparse or argparse...let us know more about what you're trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top