2

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']
user3541631
  • 3,686
  • 8
  • 48
  • 115
  • 2
    Does this answer your question? [In Python, how do I split a string and keep the separators?](https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-separators) – kotatsuyaki Oct 30 '22 at 14:29

3 Answers3

3

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']
2

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

ukBaz
  • 6,985
  • 2
  • 8
  • 31
1

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']
Arifa Chan
  • 947
  • 2
  • 6
  • 23