从python中的文件中读取一行

从python中的文件中读取一行,python,file-io,Python,File Io,我有一个名为mcelog.conf的文件,我正在代码中读取这个文件。该文件的内容如下: no-syslog = yes # (or no to disable) logfile = /tmp/logfile 程序将读取mcelog.conf文件并检查no syslog标记,如果no syslog=yes则程序必须检查标记logfile并读取logfile标记。有人能告诉我如何获得值/tmp/logfile with open('/etc/mcelog/mcelog.conf', 'r+')

我有一个名为
mcelog.conf
的文件,我正在代码中读取这个文件。该文件的内容如下:

no-syslog = yes   # (or no to disable)
logfile = /tmp/logfile
程序将读取
mcelog.conf
文件并检查
no syslog
标记,如果
no syslog=yes
则程序必须检查标记
logfile
并读取
logfile
标记。有人能告诉我如何获得值
/tmp/logfile

with open('/etc/mcelog/mcelog.conf', 'r+') as fp:
    for line in fp:
        if re.search("no-syslog =", line) and re.search("= no", line):
            memoryErrors = readLogFile("/var/log/messages")
            mcelogPathFound = true
            break
        elif re.search("no-syslog =", line) and re.search("= yes", line):
            continue
        elif re.search("logfile =", line):  
            memoryErrors = readLogFile(line)   # Here I want to pass the value "/tmp/logfile" but currently "logfile = /tmp/logfile" is getting passed
            mcelogPathFound = true
            break
fp.close()

您可以拆分该行以获得所需的值:

line.split(' = ')[1]

但是,您可能需要查看文档。

您可以拆分行以获得所需的值:

line.split(' = ')[1]
但是,您可能需要查看文档。

将代码更改为:

with open('/etc/mcelog/mcelog.conf', 'r+') as fp:
    for line in fp:
        if re.search("no-syslog =", line) and re.search("= no", line):
            memoryErrors = readLogFile("/var/log/messages")
            mcelogPathFound = true
            break
        elif re.search("no-syslog =", line) and re.search("= yes", line):
            continue
        elif re.search("logfile =", line):  
            emoryErrors = readLogFile(line.split("=")[1].strip())   # Here I want to pass the value "/tmp/logfile" but currently "logfile = /tmp/logfile" is getting passed
            mcelogPathFound = true
            break
 fp.close()
这是因为您只想读取行的一部分,而不是整个内容,所以我将其按“=”号拆分,然后将其剥离以删除任何空格

将代码更改为:

with open('/etc/mcelog/mcelog.conf', 'r+') as fp:
    for line in fp:
        if re.search("no-syslog =", line) and re.search("= no", line):
            memoryErrors = readLogFile("/var/log/messages")
            mcelogPathFound = true
            break
        elif re.search("no-syslog =", line) and re.search("= yes", line):
            continue
        elif re.search("logfile =", line):  
            emoryErrors = readLogFile(line.split("=")[1].strip())   # Here I want to pass the value "/tmp/logfile" but currently "logfile = /tmp/logfile" is getting passed
            mcelogPathFound = true
            break
 fp.close()

这是因为您只想读取行的一部分,而不是整个内容,所以我用“=”号将其拆分,然后将其剥离以删除任何空格

我喜欢
configparser
模块的建议,下面是一个示例(Python 3)

对于给定的输入,它将输出
reading/var/log/messages

import configparser, itertools
config = configparser.ConfigParser()
filename = "/tmp/mcelog.conf"

def readLogFile(filename):
    if filename:
        print("reading", filename)
    else:
        raise ValueError("unable to read file")

section = 'global'
with open(filename) as fp:
    config.read_file(itertools.chain(['[{}]'.format(section)], fp), source = filename)

no_syslog = config[section]['no-syslog']
if no_syslog == 'yes':
    logfile = "/var/log/messages"
elif no_syslog == 'no':
    logfile = config[section]['logfile']

if logfile:
    mcelogPathFound = True

memoryErrors = readLogFile(logfile)

我喜欢
configparser
模块的建议,下面是一个例子(Python 3)

对于给定的输入,它将输出
reading/var/log/messages

import configparser, itertools
config = configparser.ConfigParser()
filename = "/tmp/mcelog.conf"

def readLogFile(filename):
    if filename:
        print("reading", filename)
    else:
        raise ValueError("unable to read file")

section = 'global'
with open(filename) as fp:
    config.read_file(itertools.chain(['[{}]'.format(section)], fp), source = filename)

no_syslog = config[section]['no-syslog']
if no_syslog == 'yes':
    logfile = "/var/log/messages"
elif no_syslog == 'no':
    logfile = config[section]['logfile']

if logfile:
    mcelogPathFound = True

memoryErrors = readLogFile(logfile)

你试过正则表达式组吗?或者甚至在等号上拆分,基本上制作键值字典?不,我没有尝试过正则表达式组,如果我要用“=”号拆分每一行,那么如果文件中存在一些注释,则可能会出现异常。@板球-007为什么?你对两个答案都发表了评论,它们告诉你相同的事情。我没有尝试过正则表达式组,因为我不知道如何使用它。如果你能让我知道如何使用,那将是一个很大的帮助。我对答案进行了评论,因为有一个答案不合适,后来他修改了答案@cricket-007你试过regex乐队吗?或者甚至在等号上拆分,基本上制作键值字典?不,我没有尝试过正则表达式组,如果我要用“=”号拆分每一行,那么如果文件中存在一些注释,则可能会出现异常。@板球-007为什么?你对两个答案都发表了评论,它们告诉你相同的事情。我没有尝试过正则表达式组,因为我不知道如何使用它。如果你能让我知道如何使用,那将是一个很大的帮助。我对答案进行了评论,因为有一个答案不合适,后来他修改了答案@cricket-007当我尝试
emoryErrors=readLogFile(line).split(“=”[1])时,strip()
still readLogFile函数正在传递相同的参数“logfile=/tmp/logfile”。但是当我像
emoryErrors=readLogFile(line.split(“=”[1].strip())
那样传递时,它的工作就正常了@gaurav Dhamayah我犯了一个错误。我修改了我的答案以反映同样的情况。我希望你有这个想法。是的,我有这个想法,而且效果很好。感谢@gaurav Dhama当我尝试
emoryErrors=readLogFile(line).split(“=”[1]).strip()
still readLogFile函数正在传递相同的参数“logfile=/tmp/logfile”。但是当我像
emoryErrors=readLogFile(line.split(“=”[1].strip())
那样传递时,它的工作就正常了@gaurav Dhamayah我犯了一个错误。我修改了我的答案以反映同样的情况。我希望你有这个想法。是的,我有这个想法,而且效果很好。感谢@gaurav Dhama当我尝试
emoryErrors=readLogFile(line.split(“=”[1].strip())
时,那么它的接线就很好了。感谢@ICARTI当我尝试
emoryErrors=readLogFile(line.split(“=”[1].strip())
时,一切正常。谢谢@icart