python中的子字符串
我在python中有以下模式的字符串:python中的子字符串,python,string,substring,Python,String,Substring,我在python中有以下模式的字符串: 2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw 如何获取子字符串:C:\Scan\raisoax.exe和特洛伊木马程序Win32.VBKrypt.agqw 字符串之间是tab只需使用python字符串的substring方法即可 s = r"2011-03-01 14:10:43 C:\Scan\raisoax.exe detected T
2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw
如何获取子字符串:C:\Scan\raisoax.exe和特洛伊木马程序Win32.VBKrypt.agqw
字符串之间是tab只需使用python字符串的substring方法即可
s = r"2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw"
s.split("\t")
明白了吗
['2011-03-01 14:10:43 C:\\\\Scan\\raisoax.exe detected', 'Trojan.Win32.VBKrypt.agqw']
只需使用python字符串的substring方法
s = r"2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw"
s.split("\t")
明白了吗
['2011-03-01 14:10:43 C:\\\\Scan\\raisoax.exe detected', 'Trojan.Win32.VBKrypt.agqw']
要处理文件名中的空格,请尝试
print " ".join(v[2:-2])
要处理文件名中的空格,请尝试
print " ".join(v[2:-2])
使用重新包装。差不多
import re
s = r'2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw'
m = re.search('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s(.+)\s+detected\s+(.+)', s)
print 'file: ' + m.group(1)
print 'error: ' + m.group(2)
使用重新包装。差不多
import re
s = r'2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw'
m = re.search('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s(.+)\s+detected\s+(.+)', s)
print 'file: ' + m.group(1)
print 'error: ' + m.group(2)
使用正则表达式的解决方案:
s = "2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw"
reg = re.match(r"\S*\s\S*\s(.*)[^\ ] detected\s+(.*)",s)
file,name = reg.groups()
这将捕获包含空格的文件。如果您的文件中有“检测到”的文件,则会失败。(您也可以添加一个正向断言来修复该问题。使用正则表达式的解决方案:
s = "2011-03-01 14:10:43 C:\Scan\raisoax.exe detected Trojan.Win32.VBKrypt.agqw"
reg = re.match(r"\S*\s\S*\s(.*)[^\ ] detected\s+(.*)",s)
file,name = reg.groups()
这将捕获其中包含空格的文件。如果文件中有“检测到”的文件,则此操作将失败。(您也可以添加正向断言来修复此问题。您可以使用名为“substring”的包。只需键入“pip install substring”。只需提及开始和结束字符/索引即可获得子字符串。您可以使用名为“substring”的软件包。只需键入“pip install substring”。只需提及开始字符和结束字符/索引即可获得子字符串。如果文件路径中有空格,如
C:\Program Files\fubar.exe
?如果文件路径中有空格,如C:\Program Files\fubar.exe
?我添加了r“对于反斜杠的正确编码。对于您的问题,我真的不重要,因为重要的是拆分中的\t,用于在子字符串之间使用制表符。我添加了r“”对于反斜杠的正确编码。对于您的问题,我真的不重要,因为重要的是拆分中的\t,用于在子字符串之间使用tab作为delimeter。