在文本文件中搜索数值和条件 (Python)
Searching for numeric values and conditions in text file (Python)
我有一个格式如下的文本文件:
------------------------------------------------------------
Name=A Run=true Idle=15
Name=B Run=true Idle=10
Name=C Run=false Idle=75
Name=D Run=true Idle=92
Name=E Run=false Idle=6
Name=F Run=false Idle=21
-------------------------------------------------------------
我想在该文本文件中搜索 运行 条件 "true" 和大于设定值的空转(例如 10)。一旦满足这个条件,我想打印整行。我怎样才能得到这个?当前代码为:
def main():
with open('data.txt', 'r') as searchfile:
for line in searchfile:
if ("Run=true") in line:
if ("Idle>=10") in line:
print "\n"
print line
if __name__ == '__main__':
main()
应该这样做:
def main():
with open('data.txt', 'r') as searchfile:
for line in searchfile:
line = line.strip()
# This works because Python checks this substring in the line
if ("Run=true") in line:
firstSplit = line.split("Idle=")[1]
# Splitting again to get the first element before the whitespace
idleNum = int(firstSplit.split('\s')[0])
# This had to be changed as you are comparing integers while
# the data is still of type string
if (idleNum >=10):
print line
if __name__ == '__main__':
main()
使用以上数据的结果:
Name=A Run=true Idle=15
Name=B Run=true Idle=10
Name=D Run=true Idle=92
差不多...让我们进入最深处,如果...
此时需要提取值:
field_list = line.split(' ')
字段列表现在是三个字符串的列表,space 分隔的字段。现在,抓住最后一个并按等号拆分:
idle_field = field_list[-1].split('=')
idle_field 现在包含两个字符串:"Idle" 和数字。最后:
if int(idle_field) >= 10:
print "\n", line
有你要的条件
可以将所有处理折叠到 if 条件中,但我建议保持程序的可读性。 :-)
您正在尝试通过在字符串中搜索来比较整数。试试这个
import re
def main():
with open('data.txt', 'r') as searchfile:
for line in searchfile:
if ("Run=true") in line:
if int(re.findall(r"Idle=(.+)", line)[0]) >= 10:
print line
if __name__ == '__main__':
main()
我有一个格式如下的文本文件:
------------------------------------------------------------
Name=A Run=true Idle=15
Name=B Run=true Idle=10
Name=C Run=false Idle=75
Name=D Run=true Idle=92
Name=E Run=false Idle=6
Name=F Run=false Idle=21
-------------------------------------------------------------
我想在该文本文件中搜索 运行 条件 "true" 和大于设定值的空转(例如 10)。一旦满足这个条件,我想打印整行。我怎样才能得到这个?当前代码为:
def main():
with open('data.txt', 'r') as searchfile:
for line in searchfile:
if ("Run=true") in line:
if ("Idle>=10") in line:
print "\n"
print line
if __name__ == '__main__':
main()
应该这样做:
def main():
with open('data.txt', 'r') as searchfile:
for line in searchfile:
line = line.strip()
# This works because Python checks this substring in the line
if ("Run=true") in line:
firstSplit = line.split("Idle=")[1]
# Splitting again to get the first element before the whitespace
idleNum = int(firstSplit.split('\s')[0])
# This had to be changed as you are comparing integers while
# the data is still of type string
if (idleNum >=10):
print line
if __name__ == '__main__':
main()
使用以上数据的结果:
Name=A Run=true Idle=15
Name=B Run=true Idle=10
Name=D Run=true Idle=92
差不多...让我们进入最深处,如果... 此时需要提取值:
field_list = line.split(' ')
字段列表现在是三个字符串的列表,space 分隔的字段。现在,抓住最后一个并按等号拆分:
idle_field = field_list[-1].split('=')
idle_field 现在包含两个字符串:"Idle" 和数字。最后:
if int(idle_field) >= 10:
print "\n", line
有你要的条件
可以将所有处理折叠到 if 条件中,但我建议保持程序的可读性。 :-)
您正在尝试通过在字符串中搜索来比较整数。试试这个
import re
def main():
with open('data.txt', 'r') as searchfile:
for line in searchfile:
if ("Run=true") in line:
if int(re.findall(r"Idle=(.+)", line)[0]) >= 10:
print line
if __name__ == '__main__':
main()