Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string with multiple delimiters without deleting delimiters in Python?

Tags:

python

I currently have a list of filenames in a txt file and I am trying to sort them. The first this I am trying to do is split them into a list since they are all in a single line. There are 3 types of file types in the list. I am able to split the list but I would like to keep the delimiters in the end result and I have not been able to find a way to do this. The way that I am splitting the files is as follows:

import re

def breakLines():
    unsorted_list = []
    file_obj = open("index.txt", "rt")
    file_str = file_obj.read()

    unsorted_list.append(re.split('.txt|.mpd|.mp4', file_str))

    print(unsorted_list)

breakLines()

I found DeepSpace's answer to be very helpful here Split a string with "(" and ")" and keep the delimiters (Python), but that only seems to work with single characters.

EDIT:

Sample input:

file_name1234.mp4file_name1235.mp4file_name1236.mp4file_name1237.mp4

Expected output:

file_name1234.mp4

file_name1235.mp4

file_name1236.mp4

file_name1237.mp4

like image 835
Alexiz Hernandez Avatar asked Dec 02 '25 22:12

Alexiz Hernandez


1 Answers

In re.split, the key is to parenthesise the split pattern so it's kept in the result of re.split. Your attempt is:

>>> s = "file_name1234.mp4file_name1235.mp4file_name1236.mp4file_name1237.mp4"
>>> re.split('.txt|.mpd|.mp4', s)
['file_name1234', 'file_name1235', 'file_name1236', 'file_name1237', '']

okay that doesn't work (and the dots would need escaping to be really compliant with what an extension is), so let's try:

>>> re.split('(\.txt|\.mpd|\.mp4)', s)
['file_name1234',
'.mp4',
 'file_name1235',
 '.mp4',
 'file_name1236',
 '.mp4',
 'file_name1237',
 '.mp4',
 '']

works but this is splitting the extensions from the filenames and leaving a blank in the end, not what you want (unless you want an ugly post-processing). Plus this is a duplicate question: In Python, how do I split a string and keep the separators?

But you don't want re.split you want re.findall:

>>> s = "file_name1234.mp4file_name1235.mp4file_name1236.mp4file_name1237.mp4"
>>> re.findall('(\w*?(?:\.txt|\.mpd|\.mp4))',s)
['file_name1234.mp4',
 'file_name1235.mp4',
 'file_name1236.mp4',
 'file_name1237.mp4']

the expression matches word characters (basically digits, letters & underscores), followed by the extension. To be able to create a OR, I created a non-capturing group inside the main group.

If you have more exotic file names, you can't use \w anymore but it still reasonably works (you may need some str.strip post-processing to remove leading/trailing blanks which are likely not part of the filenames):

>>> s = " file name1234.mp4file-name1235.mp4 file_name1236.mp4file_name1237.mp4"
>>> re.findall('(.*?(?:\.txt|\.mpd|\.mp4))',s)
[' file name1234.mp4',
 'file-name1235.mp4',
 ' file_name1236.mp4',
 'file_name1237.mp4']

So sometimes you think re.split when you need re.findall, and the reverse is also true.

like image 179
Jean-François Fabre Avatar answered Dec 04 '25 11:12

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!