Split text in front of a particle/word

Question:

A normal split:

s = "/works/proj/kod/resources/excel/words/tam.xls"
p = s.split("/kod")
print(p)

Result

['/works/proj', '/resources/excel/words/tam.xls']

What I want to obtain is, a split in front of the passed particle/word.:

Example:

 ['/works/proj', '/kod/resources/excel/words/tam.xls']
Asked By: user3541631

||

Answers:

You could just append the seperator to the result afterwards:

s = "/works/proj/kod/resources/excel/words/tam.xls"
p = s.split("/kod")
p[1] = "/kod" + p[1]
print(p)

The result is this:

['/works/proj', '/kod/resources/excel/words/tam.xls']
Answered By: Thunderwrathy

You can use index slicing:

s = "/works/proj/kod/resources/excel/words/tam.xls"
char = s.index("/kod"); p = [s[:char], s[char:]]
print(p)

# ['/works/proj', '/kod/resources/excel/words/tam.xls']
Answered By: Arifa Chan

It seems like you are working with paths and files so Python’s pathlib might be helpful in this situation.

From your example, if /works/proj is a known project area and you want to know the location of the file relative to that directory you can use the following:

from pathlib import Path
proj_root = Path('/works/proj')
xls_abs = Path("/works/proj/kod/resources/excel/words/tam.xls")

xls_rel = xls_abs.relative_to(proj_root)

print(xls_rel)
# kod/resources/excel/words/tam.xls

Or if it is specifically the word kod you want to search for then you can split the path into parts and rejoin them from that index. e.g:

xls_parts = xls_abs.parts
kod_index = xls_parts.index('kod')
xls_rel = Path(*xls_parts[kod_index:])
print(xls_rel)
# kod/resources/excel/words/tam.xls

Answered By: ukBaz
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.