在Python中拆分文件路径列表。

huangapple go评论80阅读模式
英文:

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&#39;(?&lt;={os.path.sep})&#39;, p) for p in path_list]

<!-- -->

print (split_paths)
# [[&#39;path/&#39;, &#39;to/&#39;, &#39;file.txt&#39;], [&#39;path/&#39;, &#39;to/&#39;, &#39;another/&#39;, &#39;file.txt&#39;], [&#39;another/&#39;, &#39;path/&#39;, &#39;to/&#39;, &#39;afile.txt&#39;], [&#39;justafile.txt&#39;]]

More information on regex lookarounds

答案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 = [&#39;path/to/file.txt&#39;, &#39;path/to/another/file.txt&#39;, &#39;another/path/to/afile.txt&#39;, &#39;justafile.txt&#39;]
paths = [re.findall(r&#39;([^/]+/?)&#39;, p) for p in path_list]
print(paths)

[[&#39;path/&#39;, &#39;to/&#39;, &#39;file.txt&#39;], [&#39;path/&#39;, &#39;to/&#39;, &#39;another/&#39;, &#39;file.txt&#39;], [&#39;another/&#39;, &#39;path/&#39;, &#39;to/&#39;, &#39;afile.txt&#39;], [&#39;justafile.txt&#39;]]

huangapple
  • 本文由 发表于 2023年4月20日 01:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定