0

I have some table names like below in python

table1='123_test_abc_de'
table2='123_test_10_abc_de'
table3='123_red_ce'
table4='123_utc'

Now from this list I want to split the tables like below

abc_de
abc_de
red_ce
utc

I have tried like below

repo=table.split("_", 1)[-1]

I got the following result for each table

test_abc_de
test_10_abc_de
red_ce
utc

How can I achieve what I want.

1
  • Probably with a regular expression. Commented Jun 1, 2017 at 19:08

1 Answer 1

2

If the table is a list of strings like you say it is:

map(lambda x: '_'.join(x.split('_')[-2:]),table)

will give you a list of new strings like you want. Also I just found 'rsplit':

map(lambda x: '_'.join(x.rsplit('_',2)[-2:]),table)

For a single such string in a variable just use rsplit:

'_'.join(table1.rsplit('_',2)[-2:])

Long string

If all of your table names are contained in a long string, separated by space, then you just need to split that string:

map(lambda x: '_'.join(x.rsplit('_',2)[-2:]),tableNamesString.split())
Sign up to request clarification or add additional context in comments.

2 Comments

Actually table is a string not list of strings
@Virureddy I updated my answer to solve for a long string. It's unclear from your question what you actually have, where are the names coming from. Once you have a single name rsplit will do the job though. If this still isn't what you looked for please amend your question and be specific how you're retrieving the names, or in what kind of data type are you holding them. If you have one name in a different variable (which would be very odd) than you need to rsplit each.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.