python生成配置文件|怎么更改pylint的默认配置文件

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

ini文件简介ini是我们常见到的配置文件格式之一。ini是微软Windows操作系统中的文件扩展名(也常用在其他系统)。INI是英文“初始化(Initial)”的缩写。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。网络通过它,可以将经常需要改变的参数保存起来(而且还可读),使程序更加的灵活。我先给出一个ini文件的示例。[School]ip = 10.15.40.123mask = 255.255.255.0gateway = 10.15.40.1dns = 211.82.96.1[Match]ip = 172.17.29.120mask = 255.255.255.0gateway = 172.17.29.1dns = 0.0.0.0这个配置文件中保存的是不同场合下的IP设置参数。下面将以生成和读取这个配置文件为例,进行讲解。Python(v3)读取方法首先,Python读取ini配置需要用到ConfigParser包,所以要先加载它。import configparser之后我们需要载入配置文件。config=configparser.ConfigParser()#IpConfig.ini可以是一个不存在的文件,意味着准备新建配置文件。config.read("IpConfig.ini")接下来,我们可以使用configparser.add_section()向配置文件中添加一个Section。#添加节Schoolconfig.add_section("School")注意:如果文件中已经存在相应的项目,则不能再增加同名的节。然后可以使用configparser.set()在节School中增加新的参数。#添加新的IP地址参数config.set("School","IP","192.168.1.120")config.set("School","Mask","255.255.255.0")config.set("School","Gateway","192.168.1.1")config.set("School","DNS","211.82.96.1")你可以以同样的方式增加其它几项。#由于ini文件中可能有同名项,所以做了异常处理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")except configparser.DuplicateSectionError:print("Section 'Match' already exists")增加完所有需要的项目后,要记得使用configparser.write()进行写入操作。config.write(open("IpConfig.ini", "w"))以上就是写入配置文件的过程。接下来我们使用configparser.get()读取刚才写入配置文件中的参数。读取之前要记得读取ini文件。ip=config.get("School","IP")mask=config.get("School","mask")gateway=config.get("School","Gateway")dns=config.get("School","DNS")print((ip,mask+"\n"+gateway,dns))完整示例下面是一个完整的示例程序,他将生成一个IpConfig.ini的配置文件,再读取文件中的数据,输出到屏幕上。# -*- coding: utf-8 -*-import configparser#读取配置文件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")except configparser.DuplicateSectionError:print("Section 'School' already exists")#写入比赛配置文件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")except configparser.DuplicateSectionError:print("Section 'Match' already exists")#写入配置文件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+"\n"+gateway,dns))总结Python读取ini文件还是十分简单的,这里我给出的只是一些简单的使用方法,如果想用更高级的功能,比如和注释有关的功能。可以参考Pyhton官方文档

Ⅱ 怎么更改pylint的默认配置文件

获取帮助信息pylint安装成功后,可以通过运行"pylint –help"来快速查看pylint的帮助信息;相关信息基本能够支撑起快速使用起来pylint的基本功能。[email protected]:~$ pylint –helpNo config file found, using default configurationUsage: pylint [options] mole_or_package Check that a mole satisfies a coding standard (and more !). pylint –help Display this help message and exit. pylint –help-msg <msg-id>[,<msg-id>] Display help messages about given message identifiers and exit.Options: –version show program's version number and exit -h, –help show this help message and exit –long-help more verbose help. Master: –rcfile=<file> Specify a configuration file. -E, –errors-only In error mode, checkers without error messages are disabled and for others, only the ERROR messages are displayed, and no reports are done by default –ignore=<file>[,<file>…] Add files or directories to the blacklist. They should be base names, not paths. [current: CVS] Commands: –help-msg=<msg-id> Display a help message for the given message id and exit. The value may be a comma separated list of message ids. –generate-rcfile Generate a sample configuration file according to the current configuration. You can put other options before this one to get them in the generated configuration. Messages control: -e <msg ids>, –enable=<msg ids> Enable the message, report, category or checker with the given id(s). You can either give multiple identifier separated by comma (,) or put this option multiple time. See also the "–disable" option for examples. -d <msg ids>, –disable=<msg ids> Disable the message, report, category or checker with the given id(s). You can either give multiple identifiers separated by comma (,) or put this option multiple times (only on the command line, not in the configuration file where it should appear only once).You can also use "–disable=all" to disable everything first and then reenable specific checks. For example, if you want to run only the similarities checker, you can use "–disable=all –enable=similarities". If you want to run only the classes checker, but have no Warning level messages displayed, use"–disable=all –enable=classes –disable=W" Reports: -f <format>, –output-format=<format> Set the output format. Available formats are text, parseable, colorized, msvs (visual studio) and html. You can also give a reporter class, eg mypackage.mymole.MyReporterClass. [current: text] -r <y_or_n>, –reports=<y_or_n> Tells whether to display a full report or only the messages [current: yes] –msg-template=<template> Template used to display messages. This is a python new-style format string used to format the message information. See doc for all details生成配置文件可以通过"pylint –generate-rcfile"生成配置文件模板,可以在模板文件上定制相关的统一的配置文件。配置文件中包含了master, message control, reports, typecheck, similarities, basic, variables, format, design, classes, imports, exception相关的lint配置信息,用户可以进行私人订制。[email protected]:~$ cat pylint.conf[MASTER]# Specify a configuration file.#rcfile=# Python code to execute, usually for sys.path manipulation such as# pygtk.require().#init-hook=# Profiled execution.profile=no# Add files or directories to the blacklist. They should be base names, not# paths.ignore=CVS# Pickle collected data for later comparisons.persistent=yes# List of plugins (as comma separated values of python moles names) to load,# usually to register additional checkers.load-plugins=[MESSAGES CONTROL]# Enable the message, report, category or checker with the given id(s). You can# either give multiple identifier separated by comma (,) or put this option# multiple time. See also the "–disable" option for examples.#enable=# Disable the message, report, category or checker with the given id(s). You# can either give multiple identifiers separated by comma (,) or put this# option multiple times (only on the command line, not in the configuration# file where it should appear only once).You can also use "–disable=all" to# disable everything first and then reenable specific checks. For example, if# you want to run only the similarities checker, you can use "–disable=all# –enable=similarities". If you want to run only the classes checker, but have# no Warning level messages displayed, use"–disable=all –enable=classes# –disable=W"#disable=disable=logging-not-lazy, line-too-long, trailing-whitespace, bare-except, broad-except[REPORTS]# Set the output format. Available formats are text, parseable, colorized, msvs# (visual studio) and html. You can also give a reporter class, eg# mypackage.mymole.MyReporterClass.output-format=text# Put messages in a separate file for each mole / package specified on the# command line instead of printing them on stdout. Reports (if any) will be# written in a file name "pylint_global.[txt|html]".files-output=no# Tells whether to display a full report or only the messagesreports=yes# Python expression which should return a note less than 10 (10 is the highest# note). You have access to the variables errors warning, statement which# respectively contain the number of errors / warnings messages and the total# number of statements analyzed. This is used by the global evaluation report# (RP0004).evaluation=10.0 – ((float(5 * error + warning + refactor + convention) / statement) * 10)

Ⅲ python3 如何创建一个.ini的配置文件。

1、说明:python3使用configparser模块来处理ini配置文件。2、代码示例:需要生成conf.ini配置文件如下:[config]v1 = 100v2 = abcv3 = truev4 = 123.45python代码:import configparser# 加载现有配置文件conf = configparser.ConfigParser()# 写入配置文件conf.add_section('config') #添加section# 添加值conf.set('config', 'v1', '100')conf.set('config', 'v2', 'abc')conf.set('config', 'v3', 'true')conf.set('config', 'v4', '123.45')# 写入文件with open('conf.ini', 'w') as fw: conf.write(fw)# 读取配置信息v1 = conf.getint('config', 'v1')v2 = conf.get('config', 'v2')v3 = conf.getboolean('config', 'v3')v4 = conf.getfloat('config', 'v4')print('v1:', v1)print('v2:', v2)print('v3:', v3)print('v4:', v4)打开conf.ini文件检查内容

3、模块常用函数:1)读取配置文件read(filename) 直接读取ini文件内容sections() 得到所有的section,并以列表的形式返回options(section) 得到该section的所有optionitems(section) 得到该section的所有键值对get(section,option) 得到section中option的值,返回为string类型getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。2)写入配置文件add_section(section) 添加一个新的sectionset( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

Ⅳ 怎么在python中创建配置文件

读取config中info段中的name变量值.最后讲讲如何设置值.使用set(段名,变量名,值)来设置变量.config.set(''info'',''age'',''21'')表示把info段中age变量设置为21.就这么简单.


赞 (0)