广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python3.6安装ConfigPar
  • 518
分享到

Python3.6安装ConfigPar

ConfigPar 2023-01-31 01:01:29 518人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

本文转载至:Http://www.pythontab.com/html/2014/Pythonhexinbiancheng_1120/919.html 1.基本的读取配置文件 -read(filename) 直

本文转载至:Http://www.pythontab.com/html/2014/Pythonhexinbiancheng_1120/919.html

1.基本的读取配置文件

-read(filename) 直接读取ini文件内容

-sections() 得到所有的section,并以列表的形式返回

-options(section) 得到该section的所有option

-items(section) 得到该section的所有键值对

-get(section,option) 得到section中option的值,返回为string类型

-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

2.基本的写入配置文件

-add_section(section) 添加一个新的section

-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

 

3.基本例子

test.conf

1
2
3
4
5
6
7
8
9
[sec_a] 
a_key1 = 20 
a_key2 = 10 
   
[sec_b] 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = $r 
b_key4 = 127.0.0.1

parse_test_conf.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import ConfigParser 
cf = ConfigParser.ConfigParser() 
#read config 
cf.read("test.conf"
# return all section 
secs = cf.sections() 
print 'sections:', secs 
   
opts = cf.options("sec_a"
print 'options:', opts 
   
kvs = cf.items("sec_a"
print 'sec_a:', kvs 
   
#read by type 
str_val = cf.get("sec_a""a_key1"
int_val = cf.getint("sec_a""a_key2"
   
print "value for sec_a's a_key1:", str_val 
print "value for sec_a's a_key2:", int_val 
   
#write config 
#update value 
cf.set("sec_b""b_key3""new-$r"
#set a new value 
cf.set("sec_b""b_newkey""new-value"
#create a new section 
cf.add_section('a_new_section'
cf.set('a_new_section''new_key''new_value'
   
#write back to configure file 
cf.write(open("test.conf""w"))

得到终端输出:

1
2
3
4
5
sections: ['sec_b''sec_a'
options: ['a_key1''a_key2'
sec_a: [('a_key1'"i'm value"), ('a_key2', '22')] 
value for sec_a's a_key1: i'm value 
value for sec_a's a_key2: 22

更新后的test.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
[sec_b] 
b_newkey = new-value 
b_key4 = 127.0.0.1 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = new-$r 
   
[sec_a] 
a_key1 = i'm value 
a_key2 = 22 
   
[a_new_section] 
new_key = new_value

4.Python的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。 

设定配置文件test2.conf

1
2
3
4
[portal] 
url = http://%(host)s:%(port)s/Portal 
host = localhost 
port = 8080

使用RawConfigParser:

1
2
3
4
5
6
7
8
9
10
11
import ConfigParser 
  
cf = ConfigParser.RawConfigParser() 
  
print "use RawConfigParser() read" 
cf.read("test2.conf"
print cf.get("portal""url"
  
print "use RawConfigParser() write" 
cf.set("portal""url2""%(host)s:%(port)s"
print cf.get("portal""url2")

得到终端输出:

1
2
3
4
use RawConfigParser() read 
http://%(host)s:%(port)s/Portal 
use RawConfigParser() write 
%(host)s:%(port)s

改用ConfigParser:

1
2
3
4
5
6
7
8
9
10
11
import ConfigParser 
  
cf = ConfigParser.ConfigParser() 
  
print "use ConfigParser() read" 
cf.read("test2.conf"
print cf.get("portal""url"
  
print "use ConfigParser() write" 
cf.set("portal""url2""%(host)s:%(port)s"
print cf.get("portal""url2")

得到终端输出:

1
2
3
4
use ConfigParser() read 
http://localhost:8080/Portal 
use ConfigParser() write 
localhost:8080

改用SafeConfigParser:

1
2
3
4
5
6
7
8
9
10
11
import ConfigParser 
  
cf = ConfigParser.SafeConfigParser() 
  
print "use SafeConfigParser() read" 
cf.read("test2.conf"
print cf.get("portal""url"
  
print "use SateConfigParser() write" 
cf.set("portal""url2""%(host)s:%(port)s"
print cf.get("portal""url2")

得到终端输出(效果同ConfigParser):

1
2
3
4
use SafeConfigParser() read 
http://localhost:8080/Portal 
use SateConfigParser() write 
localhost:8080


Python3.6 安装ConfigParse 模块

开发环境:windows10、python3.5、Django1.11.1

第一步:首先,下载对应configparse的 .whl文件,下载地址:http://www.lfd.uci.edu/~Gohlke/pythonlibs/#simpleJSON

第二步:打开cmd,进入到Python安装目录的Scripts文件夹中.比如:D:\Program Files\Python\Scripts。使用pip安装刚刚下载好的whl文件,pip.exe install *.whl,例如:

D:\Program Files\python\Scripts>pip.exe install D:\python\configparser-3.5.0-py2.py3-none-any.whl
Processing d:\python\configparser-3.5.0-py2.py3-none-any.whl
Installing collected packages: configparser
Successfully installed configparser-3.5.0

提示安装成功后,在\Python\Lib\site-packages目录下可以看到configparse.

--结束END--

本文标题: Python3.6安装ConfigPar

本文链接: https://www.lsjlt.com/news/184341.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python3.6安装ConfigPar
    本文转载至:http://www.pythontab.com/html/2014/pythonhexinbiancheng_1120/919.html 1.基本的读取配置文件 -read(filename) 直...
    99+
    2023-01-31
    ConfigPar
  • Centos6X安装python3.6
    下载包去官网下载最新的python安装包官网地址: https://www.python.org/downloads/ 安装python3.6可能使用的依赖yum install openssl-devel bzip2-devel expa...
    99+
    2023-01-30
    Centos6X
  • Ubuntu安装Python3.6
    1、配置软件仓库sudo add-apt-repository ppa:jonathonf/python-3.62、检查系统软件包并安装Python3.6sudo apt-get updatesudo apt-get install py...
    99+
    2023-01-31
    Ubuntu
  • Linux安装python3.6
    Linux下安装Python3.6和第三方库如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境,比如yum!!!!!不要动现有的python2环境!一、安装py...
    99+
    2023-01-31
    Linux
  • python3.6 安装pyhook_
    首先是根据网上的资料来安装的 第一次是通过swig形式的安装 但是一直不行被迫无奈只能找另外一个方法通过文件形式安装 安装pyhook3的时候自己系统是64位的所以下载了python_hdf4‑0.9.1‑cp36‑cp36m‑win...
    99+
    2023-01-31
  • CentOS7 安装 Python3.6
    先看编译安装,编译安装后的文件目录基本和Windows平台上目录差不多。指定安装路径的话,所有的文件都是在一起的。后面有yum的安装的方法。 编译安装,会装好setuptools和pip这两个工具,都在site-packages这个文件夹...
    99+
    2023-01-31
  • Centos7 安装Python3.6.
    一、centos7 安装 Python3.6.5教程1、在安装Python之前,需要先安装一些后面遇到的依赖问题(如果有依赖问题,按照提示安装):    yum -y install zlib-devel bzip2-devel opens...
    99+
    2023-01-31
  • centos7 安装python3.6及
    环境 默认centos7的python版本是python2.7,并且没有安装ipython 安装python3.6 安装依赖 # yum install xz gcc zlib zlib-devel wget sqlite-devel o...
    99+
    2023-01-31
  • Centos下安装Python3.6
    一、安装python3.6.11、安装依赖环境#yum install readline-devel ##必须安装否则会出现python3编译器中不能使用退格键和方向键2、下载安装包并解压[root@bogon ~]# wget ht...
    99+
    2023-01-31
    Centos
  • centos 7 安装python3.6
    centos7 默认安装了python2.7.5,当需要使用python3的时候,可以手动下载python源码后编译安装.python 官网:www.python.org1.安装python可能用到的依赖yum install openss...
    99+
    2023-01-31
    centos
  • ubuntu16.04怎样安装python3.6
    ubuntu16.04安装python3.6的方法:打开终端命令行模式。输入以下命令将下载源加入到系统的源列表。sudo add-apt-repository ppa:jonathonf/python-3.6再输入以下命令更新列表进行更新。...
    99+
    2022-10-06
  • CentOS 7下安装Python3.6
    •安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel   •到python...
    99+
    2023-01-30
    CentOS
  • python3.6安装报错ZipImpo
    1.正常安装过程官网下载源码包 tar -zxvf Python-3.6.3.tgz cd Python-3.6.3 ./configure make make install `make install`时报错: zipimport.Zi...
    99+
    2023-01-31
    报错 ZipImpo
  • Centos下安装Python3.6和
    写在前面 centos6.8中默认自带的python版本为python2.6,那么这里需要将其改为python3 下载并解压 官方下载地址为 https://www.python.org/downlo... ,这里已3.6.3为例。 # ...
    99+
    2023-01-31
    Centos
  • python3.6中安装numpy,pa
    运行环境:python3.6+windows64位1.安装pip(1)如果在安装python3.6时,你有勾选关于pip的选项,那么在python3,6中就会带有pip的安装文件安装方法:主要下载地址:http://www.lfd.uci....
    99+
    2023-01-31
    numpy pa
  • CentOS编译安装Python3.6.
    1.获取编译包:wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz wget http://mirrors.sohu.com/python/3.6.4/Python-3...
    99+
    2023-01-31
    CentOS
  • CentOS7安装python3.6保留
    安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel ...
    99+
    2023-01-31
  • Pycharm之如何安装cv2[python3.6]
    目录Pycharm安装cv2 [python3.6]下载安装python安装cv2包报错解决总结Pycharm安装cv2 [python3.6] python解释器为Anaconda...
    99+
    2023-05-18
    Pycharm安装cv2 Pycharm安装 Pycharm cv2安装
  • 利用Anaconda安装python3.6环境
    一、首先检车Anaconda的版本 win+r  2、输入conda --version,回车 注意:3.6的对应的是 Anaconda3-5.2,5.3以后的都是python 3.7的不要看错了  3、下载地址 官方地址:Index ...
    99+
    2023-09-18
    python 深度学习 开发语言
  • Linux下Python3.6的安装步骤
    这篇文章主要介绍“Linux下Python3.6的安装步骤”,在日常操作中,相信很多人在Linux下Python3.6的安装步骤问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Linux下Python3.6的安...
    99+
    2023-06-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作