我在下面有文本文件:
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\
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
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
Run Code Online (Sandbox Code Playgroud)
我的出门是 [[],
['C:\\RegularExpressionsWithDotNet\\robocopytest\\source\\?????\\??????\\English'],
[],
[],
['C:\\RegularExpressionsWithDotNet\\robocopytest\\source\\?????\\??????\\English'],
[]]
有什么办法可以在使用re.findall时删除空列表
如果我在做 …
我的代码。我写了旋转列表的代码
s = 'abc'
lst = list(s)
for x in range(0,len(lst)):
lst = lst[-x:] + lst[:-x]
print (lst)
Run Code Online (Sandbox Code Playgroud)
我的出
['a', 'b', 'c']
['c', 'a', 'b']
['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
预期中
['a', 'b', 'c']
['c', 'a', 'b']
['b', 'c', 'a']
Run Code Online (Sandbox Code Playgroud) 我下面有一个文本文件,如果行的最后一部分是数字,则尝试提取字符串
4:16:09PM - xx yy DOS activity from 10.0.0.45
9:43:44PM - xx yy 1A disconnected from server
2:40:28AM - xx yy 1A connected
1:21:52AM - xx yy DOS activity from 192.168.123.4
Run Code Online (Sandbox Code Playgroud)
我的密码
with open(r'C:\Users\Desktop\test.log') as f:
for line in f:
dos= re.findall(r'\d',line.split()[-1])
print (list(dos))
Run Code Online (Sandbox Code Playgroud)
我的出
['1', '0', '0', '0', '4', '5']
[]
[]
['1', '9', '2', '1', '6', '8', '1', '2', '3', '4']
Run Code Online (Sandbox Code Playgroud)
预期
['10.0.0.45','192.168.123.4']