英文:
Split a list of file paths in python
问题
以下是翻译好的代码部分:
path_list = ['path/to/file.txt', 'path/to/another/file.txt', 'another/path/to/afile.txt', 'justafile.txt']
new_list = []
for i in path_list:
new_list.append(Path(i).parts)
如果您需要进一步的帮助,请告诉我。
英文:
I have a list of file path strings. I want to end up with a list of split file path strings.
before:
path_list = ['path/to/file.txt', 'path/to/another/file.txt', 'another/path/to/afile.txt', 'justafile.txt']
desired output:
[['path/', 'to/', 'file.txt'],
['path/', 'to/', 'another/', 'file.txt'],
['another/', 'path/', 'to/', 'afile.txt'],
['justafile.txt']]
So far I have tried the following:
path_list = ['path/to/file.txt', 'path/to/another/file.txt', 'another/path/to/afile.txt', 'justafile.txt']
new_list = []
for i in path_list:
new_list.append(Path(i).parts)
It is close, but loses the slashes (/).
答案1
得分: 1
You don't need the slashes in your path parts. And if you really do, then you could simply append os.path.sep
to each part.
With that said, you can use re.split
with a fixed backward-looking pattern that retains the separator:
import re
import os
split_paths = [re.split(r'(?<=' + os.path.sep + r')', p) for p in path_list]
print(split_paths)
# [['path/', 'to/', 'file.txt'], ['path/', 'to/', 'another/', 'file.txt'], ['another/', 'path/', 'to/', 'afile.txt'], ['justafile.txt']]
More information on regex lookarounds
英文:
IMO you don't need the slashes in your path parts. And if you really do, then you could simply append os.path.sep
to each part.
With that said, you can use re.split
with a fixed backward looking pattern that retains the separator:
import re
import os
split_paths = [re.split(f'(?<={os.path.sep})', p) for p in path_list]
<!-- -->
print (split_paths)
# [['path/', 'to/', 'file.txt'], ['path/', 'to/', 'another/', 'file.txt'], ['another/', 'path/', 'to/', 'afile.txt'], ['justafile.txt']]
答案2
得分: 0
With re.findall
匹配:
import re
path_list = ['path/to/file.txt', 'path/to/another/file.txt', 'another/path/to/afile.txt', 'justafile.txt']
paths = [re.findall(r'([^/]+/?)', p) for p in path_list]
print(paths)
[['path/', 'to/', 'file.txt'], ['path/', 'to/', 'another/', 'file.txt'], ['another/', 'path/', 'to/', 'afile.txt'], ['justafile.txt']]
英文:
With re.findall
matching:
import re
path_list = ['path/to/file.txt', 'path/to/another/file.txt', 'another/path/to/afile.txt', 'justafile.txt']
paths = [re.findall(r'([^/]+/?)', p) for p in path_list]
print(paths)
[['path/', 'to/', 'file.txt'], ['path/', 'to/', 'another/', 'file.txt'], ['another/', 'path/', 'to/', 'afile.txt'], ['justafile.txt']]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论