我有以下代码:
with open('/home/adiel/log', 'r') as f:
for line in [[x.split()[3], x.split()[4], x.split()[5], x.split()[6]] for x in f]:
print(line)
它工作正常,并为我打印这些值:
['172.18.0.124', '172.18.0.5', '3306', '39064']
['172.18.0.124', '172.18.0.5', '3306', '62717']
['172.18.0.5', '172.18.0.124', '52909', '3306']
['172.18.0.5', '172.18.0.124', '13989', '3306']
但我想将x.split()[5]
与443
或65535
之类的值进行比较,并且只有匹配它们时才打印线条。
我该怎么做?
感谢的
答案 0 :(得分:1)
您可以使用拆分创建生成器,然后打印过滤后的值:
--[[ Given the current temperature, return the appropriate
string for the main string meter ]]
local function getMainString( temp )
local negation = (temp > Settings.Ss_Limit) and " don't" or ""
--[[ this is used to produce "You don't need a cloth" when
temp is greater than Ss_Limit. Adjust the string based on your own need.
]]
local clothwear = (temp > Settings.Ss_Limit) and "cloth"
--[[ changed < to <=, the following is the same, as not to get an error
when temp equals any of the _Limit .
]]
local summerwear = (temp <= Settings.Ss_Limit) and (temp > Settings.Vest_Limit) and "shirt and shorts"
local innerwear = (temp <= Settings.Vest_Limit) and (temp > Settings.Jacket_Limit) and "vest"
local southerwear = (temp <= Settings.Jacket_Limit) and (temp > Settings.Coat_Limit) and "jacket"
local outerwear = (temp <= Settings.Coat_Limit) and "coat"
--[[ added clothwear here, to produce proper output
when temp is greater than Ss_Limit
]]
return string.format("You%s need a %s", negation, (clothwear or summerwear or innerwear or southerwear or outerwear))
end
或者使用for循环到with open('/home/adiel/log', 'r') as f:
iter_lines = (x.split() for x in f)
for line in (x[3:7] for x in iter_lines if x[5] in {'443', '65535'}):
print(line)
行并在符合条件时打印:
split()