Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文件行中找到匹配项,然后转到下一个文件_Python_Python 2.7 - Fatal编程技术网

Python 在文件行中找到匹配项,然后转到下一个文件

Python 在文件行中找到匹配项,然后转到下一个文件,python,python-2.7,Python,Python 2.7,我是python新手,我正在尝试编写一个脚本,循环遍历my/home目录中的所有.txt文件,并在每个.txt文件中迭代行,看看是否可以找到匹配项,如果找到了,应该转到下一个文件。如果在整个文件中找不到匹配项,则报告它 我拥有的代码: strname = "ntpq -p" for file in glob.glob("/home/xxx/*.txt"): with open(file, 'rb') as f: iFoundTheLine = 0 for

我是python新手,我正在尝试编写一个脚本,循环遍历my/home目录中的所有.txt文件,并在每个.txt文件中迭代行,看看是否可以找到匹配项,如果找到了,应该转到下一个文件。如果在整个文件中找不到匹配项,则报告它

我拥有的代码:

strname = "ntpq -p"

for file in glob.glob("/home/xxx/*.txt"):
    with open(file, 'rb') as f:
        iFoundTheLine = 0
        for line in f:
            line = line.rstrip()
            id = line.split(b"|")[0]

            if strname in line:
                iFoundTheLine = 1
                print ("%s is ok" % id)
                break

        if iFoundTheLine == 0:
            print ("Problem with NTP%s" % id)
我得到的结果:

srv29393正常

NTP的问题

srv29494正常

NTP的问题

srv29399正常

srv29493正常

我期待的结果:

srv29393正常

NTP srv1234的问题

srv29494正常

NTP srv2345的问题

srv29399正常

srv29493正常

样本行:

srv29393 | 06/23/18 | 05:32:02 | ps-eo用户、pid、ppid、启动、时间、cmd | egrep-v[\wnobody 22972 21597 03:06:12 8767(dnsserver)

srv29393 | 06/23/18 | 05:32:02 | 1529746322 | ps-eo用户、pid、ppid、启动、时间、cmd | egrep-v[\wnobody 22973 21597 03:06:12 8767(dnsserver)

srv29393 | 06/23/18 | 05:32:02 | ps-eo用户、pid、ppid、启动、时间、cmd | egrep-v[\wnobody 22974 21597 03:06:12 8767(dnsserver)

srv29393 | 06/23/18 | 05:32:02 | 1529746322 |/usr/sbin/ntpq-p*1.1.1.11 11.11.11 3 u 1055 1024 377 719.042-0.625 0.016

srv29393 | 06/23/18 | 05:32:02 | 1529746322 |/usr/sbin/ntpq-p+2.2.2.11 12.12.11 3 u 1049 1024 377 824.784 0.707 0.121


对于NTP有问题的人,它缺少“id”。是否有人可以提供帮助/建议?

您有一个小问题。您试图在
for
循环外部打印
id
,而
id
无法访问。因此,它应该放在循环内部

此外,由于这个原因,没有理由使用
iFoundTheLine
变量,因为它是不必要的

编辑:
对于f中的行
应该是
对于f中的行。readlines()

通过对代码进行一些重构:

import glob
strname = "ntpq -p"

for File in glob.glob("/home/xxx/*.txt"):
    with open(File, 'r', encoding='utf-8') as f:
        for line in f.readlines():
            line = line.rstrip()
            id = line.split("<|>")[0]

            if strname in line:
                print ("%s is ok" % id)
                break

            else:
                # Since, `id` is accessible inside the loop 
                print ("Problem with NTP%s" % id)
导入全局
strname=“ntpq-p”
对于glob.glob(“/home/xxx/*.txt”)中的文件:
打开时(文件'r',编码='utf-8')为f:
对于f.readlines()中的行:
line=line.rstrip()
id=行分割(“”[0]
如果strname在行中:
打印(“%s正常”%id)
打破
其他:
#因为,`id`在循环中是可访问的
打印(“NTP%s”%id有问题)

由于文本文件包含特殊字符,我建议对该文件使用
UTF-8
编码,而不是以二进制方式访问它,因为它使文件交互更容易。

hi@hiroprotation,感谢您的指出。我修复了缩进。您是否发现了ID丢失的其他问题?如果有,请输入最后一个For循环中的station也不起作用:(我知道了…你有一个小问题。
iFoundTheLine=0
应该在
for
循环中。
if
语句可以。请在文本文件中发布一些示例行…@beeloo你应该发布这些示例行问题。很难将它们作为注释来阅读。@melvinaraham,我尝试了你的代码,我得到了一切。)由于失败:(NTPba29393有问题NTPba29392有问题NTPba29494有问题NTPba29391有问题NTPba29399有问题NTPba29493@beeloo请在问题本身中发布示例文本文件内容,因为它是可读的,并且允许在本地测试代码。@beeloo使用此更改测试代码:
用于f.readl中的行ines()
代替了f中的行的
添加了示例lines@beeloo文本文件是否有多行?