python读取配置文件|使用python的beautifulsoup读取xml配置文件

python读取配置文件|使用python的beautifulsoup读取xml配置文件的第1张示图

Ⅰ python 如何读取复杂文件 比如我有个配置文件, 格式为 “#文本内容 配置参数“ 这样的格式 该怎么读取

f=open("config.ini")for line in f: if line[0]=="#": continue elif line[0]==";": continue else: print line忽略掉#和;开头的行就可以得配置参数了。

Ⅱ 如何使用Python3读取配置文件

如果你的配置文件是文本文件,那个只能通过open去读取,获取文件里面的内容。

如果是py文件,可以直接importpy文件读取

Ⅲ 如何使用Python3读写INI配置文件

import configparser# 生成config对象conf =configparser.ConfigParser()# 用config对象读取配置文件conf.read("xxx.ini") #从ini配置文件中获取信息a=conf.get("xx","yy")print(a) 结果为: zzxxx.ini[xx]yy=zz

Ⅳ python 读取配置文件与写成函数哪个更有效率

本章节使用如下的配置文件作为示例,可在 D 盘下新建 Pyhton_config 文件夹,创建两个文件 test.config 及 test.ini 内容及示例截图如下:1 [db]2 db_port = 33063 db_user = root4 db_host = 127.0.0.15 db_pass = xgmtest6 7 [concurrent]

Ⅳ python3 怎么读取mysql配置文件

记录一个读取my.cnf配置的脚本,可以基于该函数做一些mysql 后端运维工作。各位可以基于自己的需求进行修改。脚本名称getcnf.py

importsys

importos

defread_cnf(cnf_path):

assertcnf_pathisnotNoneandos.path.exists(cnf_path)

cnf_dict={}

cur_section=None

with open(cnf_path)as cnf_reader:

forlineincnf_reader.readlines():

line=''.join(line.split())

iflen(line)<=0or'#'==line[0]:

continue

if'['==line[0]and']'==line[-1]:

cur_section=line[len('['):len(line)-1]

ifcur_sectionnotincnf_dict:

cnf_dict[cur_section]={}

elif'='inlineandline.count('=')==1:

ifcur_sectionisNone:

LOGGER.warning('cur_section is None')

continue

tokens=line.split('=')

key=tokens[0].replace('"','').replace("'",'')

value=tokens[1].replace('"','').replace("'","")

cnf_dict[cur_section][key]=value

returncnf_dict

defmain():

cnf_path="/u01/my3353/my.cnf"

mycnf=read_cnf(cnf_path)

printmycnf['mysqld']['tmpdir']

if__name__=='__main__':

main()

运行效果图

Ⅵ 使用python的beautifulsoup读取xml配置文件

beautifulsoup分析html非常方便,但xml却不怎么样

推荐使用自带的xml的etree分析版

importxml.etree.ElementTreeasETroot=ET.fromstring('''<settings><ddkey="d1"value="大一"/><ddkey="d2"value="大二权"/><ddkey="d3"value="大三"/></settings>''')root[0].items()

Ⅶ 如何使用Python3读取配置文件

完整示例下面是一个完整的示例程序,他将生成一个IpConfig.ini的配置文件,再读取文件中的数据,输出到屏幕上。#-*-coding:utf-8-*-importconfigparser#读取配置文件config=configparser.ConfigParser()config.read("IpConfig.ini")#写入宿舍配置文件try:config.add_section("School")config.set("School","IP","10.15.40.123")config.set("School","Mask","255.255.255.0")config.set("School","Gateway","10.15.40.1")config.set("School","DNS","211.82.96.1")exceptconfigparser.DuplicateSectionError:print("Section'School'alreadyexists")#写入比赛配置文件try:config.add_section("Match")config.set("Match","IP","172.17.29.120")config.set("Match","Mask","255.255.255.0")config.set("Match","Gateway","172.17.29.1")config.set("Match","DNS","0.0.0.0")exceptconfigparser.DuplicateSectionError:print("Section'Match'alreadyexists")#写入配置文件config.write(open("IpConfig.ini","w"))ip=config.get("School","IP")mask=config.get("School","mask")gateway=config.get("School","Gateway")dns=config.get("School","DNS")print((ip,mask+""+gateway,dns))

Ⅷ python 怎么读取配置文件

python 读取配置文件方法#coding=utf8 import ConfigParser config = ConfigParser.ConfigParser()config.readfp(open(raw_input("input file name:"), "rb"))print config.get("global", "ip")config.ini[global]ip = 192.168.1.100 ;ip地址port = 3306

Ⅸ python为什么没有像java的spring那样读取xml等配置文件

spring自己也在放弃xml配置吧。 都用springboot读配置了。

Ⅹ 如何用python来修改配置文件conf

楼上的俩人回答综合一下就是完美的答案,文件打开方式file.open。但是这种方式比较low,但是如果就是一简单的读写文件用着方便,configparser是专门的conf库,有一点点(只是一点点)学习成本,但是也很方便。推荐后者。

未经允许不得转载:山九号 » python读取配置文件|使用python的beautifulsoup读取xml配置文件

赞 (0)