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