python按照指定字符串打印出这个字符串的后面几行中的特定字符串开头的行。

比如我有一个文件:a.txt,内容如下:
[test1]
comment = This is test
path = /test1/test
valid users = test
write list = test
admin users = test
browseable = yes
public = no

[test]
comment = This is test
path = /test1/test
valid users = test1
write list = test1
admin users = test1
browseable = yes
public = no

怎么样按照[test] 这个字符串打印出path = /test1/test 这一行,不能按行号来打印,因为可能有多段,求大神指导。

Python Code:

from configparser import ConfigParser
cfgParser = ConfigParser()
cfgParser.read(r"D:\a.txt") # 路径修改为你电脑中的即可
cfgParser.get("test", "path")

输出:'/test1/test'

追问

没有输出,

追答

Linux 系统还是加个 print 保险:

print(cfgParser.get("test", "path"))

我这边是 Windows 系统,不加 print 可以直接输出:

追问

谢谢!!

温馨提示:答案为网友推荐,仅供参考
相似回答