【发布时间】:2021-01-04 13:03:15
【问题描述】:
我有一个目录列表作为 python 中的列表,其中包含以下数据:
/a
/a/b
/a/b/1
/a/b/2
/a/b/3
/a/c
/a/c/1
/a/c/2
/a/c/3
/a/d
/a/d/1
/a/d/2
/f
/f/g
/f/g/h
/f/g/i
/f/g/j
我想以以下格式显示输出:
/a
/b
/1
/2
/3
/c
/1
/2
/3
/d
/1
/2
/f
/g
/h
/i
/j
我正在使用以下 python 函数,但它打印的输出不一致:
def formatVFS(lst):
for line in lst:
l1 = list(lst)
l2 = []
if re.search(r'^/\w+', line) != None:
temp = re.search(r'^/\w+', line).group()
print '\t\t', temp
for line2 in l1:
if line2.startswith(temp):
lst.remove(line2)
line2 = line2.replace(temp, '')
if line2!= '':
l2.append(line2)
print '\t\t\t',line2
formatVFS(l2)
有什么建议吗?
【问题讨论】:
-
“它正在打印不一致的输出”请在您的问题中包含它
-
另附注:
print '\t...是 python2 语法。 Python 2 reached EOL earlier this year. 除非你有充分的理由使用 Python 2,否则你应该使用 Python 3。 -
我必须在遗留系统中使用这个程序。所以不能改变Python的版本。安装的python版本是2.7.5
-
这是使用 Python 2 的好理由 :)
标签: python regex for-loop directory