gpt4 book ai didi

Python删除字符直到字母或数字

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:14 24 4
gpt4 key购买 nike

我正在编写一个脚本,该脚本读取包含 Linux 树输出的文件,并且我想删除每行开头的树格式。不过,我想保留字符串中第一个字母或数字之后的间距。

这是我到目前为止所拥有的:

import re
with open(tree_loc) as f:
for line in f:
if 'batman' in line:
line = re.sub(r'[^\w*]', '', line)
print(line)

最佳答案

非正则表达式解决方案怎么样?

>>> from string import letters, digits
>>> from itertools import dropwhile
>>>
>>> keep = set(letters + digits)
>>> s = '$@@^test123'
>>> ''.join(dropwhile(lambda c: c not in keep, s))
'test123'

关于Python删除字符直到字母或数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43351780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com