Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a mixed string with numbers

I have a data in a text file that contains "Test DATA_g004, Test DATA_g003, Test DATA_g001, Test DATA_g002".

Is it possible to sort it without the word "Test DATA_" so the data will be sorted like g001, g002, g003 etc?

I tried the .split("Test DATA_") method but it doesn't work.

def readFile():
    #try block will execute if the text file is found
    try:
        fileName = open("test.txt",'r')
        data = fileName.read().split("\n")
        data.sort (key=alphaNum_Key) #alternative sort function
        print(data)
    #catch block will execute if no text file is found
    except IOError:
        print("Error: File do not exist")
        return

#Human sorting
def alphaNum(text):
    return int(text) if text.isdigit() else text

#Human sorting
def alphaNum_Key(text):
    return [ alphaNum(c) for c in re.split('(\d+)', text) ]
like image 478
Aurora_Titanium Avatar asked Dec 30 '15 17:12

Aurora_Titanium


Video Answer


2 Answers

You can do this using re.

import re
x="Test DATA_g004, Test DATA_g003, Test DATA_g001, Test DATA_g002"
print sorted(x.split(","),key= lambda k:int(re.findall("(?<=_g)\d+$",k)[0]))

Output:[' Test DATA_g001', ' Test DATA_g002', ' Test DATA_g003', 'Test DATA_g004']

like image 124
vks Avatar answered Sep 29 '22 17:09

vks


Retrieve all strings starting with g and then sort the list with sorted

>>> s = "Test DATA_g004, Test DATA_g003, Test DATA_g001, Test DATA_g002, "
>>> sorted(re.findall(r'g\d+$', s))
['g001', 'g002', 'g003', 'g004']

Another way, is to use only built-in methods:

>>> l = [x.split('_')[1] for x in s.split(', ') if x]
>>> l
['g004', 'g003', 'g001', 'g002']
>>> l.sort()
>>> l
['g001', 'g002', 'g003', 'g004']
like image 24
Iron Fist Avatar answered Sep 29 '22 18:09

Iron Fist