gpt4 book ai didi

python - 如何将re.findall应用于文件为空列表

转载 作者:行者123 更新时间:2023-12-04 18:00:27 25 4
gpt4 key购买 nike

我在下面有文本文件:

Is there any way to do removing the empty list while using re.findall


Started : Sunday, May 1, 2016 1:59:16 PM
Source : C:\RegularExpressionsWithDotNet\robocopytest\source\தமிழ்\हिन्दी\English
Dest : C:\RegularExpressionsWithDotNet\robocopytest\destn\
Started : Sunday, May 1, 2016 1:59:16 PM
Source : C:\RegularExpressionsWithDotNet\robocopytest\source\தமிழ்\हिन्दी\English
Dest : C:\RegularExpressionsWithDotNet\robocopytest\destn\


我的代码如下:

with open(r'C:\debug.log', 'r', encoding='utf-8') as fr:
eachline = fr.read()
data = [eachline.strip() for eachline in eachline.splitlines()]
datalinelist = []
for i in data:
datalinelist.append(re.findall(r'\bSource : (.+)', i))
datalinelist


我的出门是 [[],
['C:\\RegularExpressionsWithDotNet\\robocopytest\\source\\தமிழ்\\हिन्दी\\English'],
[],
[],
['C:\\RegularExpressionsWithDotNet\\robocopytest\\source\\தமிழ்\\हिन्दी\\English'],
[]]


有什么办法可以在使用re.findall时删除空列表

如果我在做 [line for line in open(file,'r',encoding='utf-8') if re.findall(r'Source : .+',line)]

我得到了正确的 ['Source : C:\\RegularExpressionsWithDotNet\\robocopytest\\source\\தமிழ்\\हिन्दी\\English\n',
'Source : C:\\RegularExpressionsWithDotNet\\robocopytest\\source\\தமிழ்\\हिन्दी\\English\n']


处理 with条件时有什么错误

最佳答案

with open(r'debug.log', 'r', encoding='utf-8') as fr:
eachline = fr.read()
data = [eachline.strip() for eachline in eachline.splitlines()]
datalinelist = []
for i in data:
datalinelist.extend(re.findall(r'\bSource : (.+)', i))
datalinelist


['C:\ RegularExpressionsWithDotNet \ robocopytest \ source \ ????? \ ?????? \ English',
'C:\ RegularExpressionsWithDotNet \ robocopytest \ source \ ????? \ ?????? \ English']

关于python - 如何将re.findall应用于文件为空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58787342/

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