Python split filename by delimiter

First I would extract filename itself. I'd split it from the extension. You can go easy way by doing:

path = "Planning_Group_20180108.ind"
filename, ext = path.split(".")

It is assuming that path is actually only a filename and extension. If I'd want to stay safe and platform independent, I'd use os module for that:

fullpath = "this/could/be/a/full/path/Planning_Group_20180108.ind"
path, filename = os.path.split(fullpath)

And then extract "root" and extension:

root, ext = os.path.splitext(filename)

That should leave me with Planning_Group_20180108 as root. To discard "_20180108" we need to split string by "_" delimiter, going from the right end, and do it only once. I would use .rsplit() method of string, which lets me specify delimiter, and number of times I want to make splits.

what_i_want, the_rest = root.rsplit("_", 1)

what_i_want should contain left side of Planning_Group_20180108 cut in place of first "_" counting from right side, so it should be Planning_Group

The more compact way of writing the same, but not that easy to read, would be:

what_i_want = os.path.splitext(os.path.split("/my/path/to/Planning_Group_20180108.ind")[1])[0].rsplit("_", 1)

PS. You may skip the part with extracting root and extension if you're sure, that extension will not contain underscore. If you're unsure of that, this step will be necessary. Also you need to think of case with multiple extensions, like /path/to/file/which_has_a.lot.of.periods.and_extentions. In that case would you like to get which_has_a.lot.of.periods.and, or which_has? Think of it while planning your app. If you need latter, you may want to extract root by doing filename.split(".", 1) instead of using os.path.splitext()

reference:

os.path.split(path),

os.path.splitext(path)

str.rsplit(sep=None, maxsplit=-1)

I have 2 shapefiles, each one placed in a different folder:

  • "C:\...\Folder O\Germany (O).shp"
  • "C:\...\Folder P\Germany Berlin (P).shp"

The following script finds the shapefiles and contains a couple of if conditions. The first if condition strips the "(O).shp" and "(P).shp" from the shapefiles. This works perfectly.

I want to set up a second if condition whereby the first name of both shapefiles matches, so in this case it would be "Germany". I thought the string.split method would be ideal but I'm clearly not implementing it correctly.

Can you point out my mistake?

def run():
    for o in glob.glob("C:\Users\me\Desktop\Test\Folder O" + "\*.shp"):
        for p in glob.glob("C:\Users\me\Desktop\Test\Folder P" + "\*.shp"):
            if os.path.basename(o).rstrip("(O).shp") == os.path.basename(p).rstrip("(P).shp"):
                print p
                # works perfectly up to here

                if os.path.basename(o).split(o[[1]]) == os.path.basename(p).split(p[1]]):
                    print p

The split() method in Python returns a list of the words in the string/line , separated by the delimiter string. This method will return one or more new strings. All substrings are returned in the list datatype.

Syntax

ParameterDescription
separator The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
maxsplit It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit.
return The split() breaks the string at the separator and returns a list of strings.

If no separator is defined when you call upon the function, whitespace will be used by default. In simpler terms, the separator is a defined character that will be placed between each variable. The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string .

Splitting String by space

The split() method in Python without an argument splits on whitespace.

example

output

Splitting on first occurrence

In the following example, it will Split by first 2 whitespace only.

example

output

Splitting lines from a text file in Python

The following Python program reading a text file and splitting it into single words in python

example

Splitting String by newline(\n)

output

Splitting String by tab(\t)

output

Splitting String by comma(,)

output

Split string with multiple delimiters

In this case Python uses Regular Expression.

example

output

Split a string into a list

The following Python program split a string to a List.

example

output

maxsplit parameter

Split the string into a list with max 2 items

output

In the above program maxsplit is 2, the first two string are split and rest of them are in a same string.

Split a string into array of characters

output

Python split() using substring

Extact a string after a specific substring.

Python split filename by delimiter

In the above example, you can see the split() function return next part of a string using a specific substring.

Python split filename by delimiter

Here, you can see the split() function return the previous part of the string using a specific substring.

Looking for a Python job ?

Chances are you will need to prove that you know how to work with Python. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming . Here are the top objective type sample Python Interview questions and their answers are given just below to them. These sample questions are framed by our experts team who trains for Python training to give you an idea of type of questions which may be asked in interview.

Go to... Python Interview Questions



How do you split a file name and extension in Python?

We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values - root and extension.

How do you split a filename underscore in Python?

You can use the Python string split() function to split a string (by a delimiter) into a list of strings. To split a string by underscore in Python, pass the underscore character "_" as a delimiter to the split() function.

How do you split a list of files in Python?

Example 1: Using the splitlines() the read() method reads the data from the file which is stored in the variable file_data. splitlines() method splits the data into lines and returns a list object. After printing out the list, the file is closed using the close() method.

How do you split a text file in Python?

The fastest way to split text in Python is with the split() method. This is a built-in method that is useful for separating a string into its individual parts. The split() method will return a list of the elements in a string.