• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python读写ini文件示例(python读写文件)

python读写ini文件示例(python读写文件)

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python读取ini,python读取ini文件,python 解析ini,python解析ini文件,python ini等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

很类似java的properties文件

xml文件

对应的python代码

#---------------------
import sys,os
import ConfigParser

class Db_Connector:
  def __init__(self, config_file_path):
    cf = ConfigParser.ConfigParser()
    cf.read(config_file_path)

    s = cf.sections()
    print 'section:', s

    o = cf.options("baseconf")
    print 'options:', o

    v = cf.items("baseconf")
    print 'db:', v

    db_host = cf.get("baseconf", "host")
    db_port = cf.getint("baseconf", "port")
    db_user = cf.get("baseconf", "user")
    db_pwd = cf.get("baseconf", "password")

    print db_host, db_port, db_user, db_pwd

    cf.set("baseconf", "db_pass", "123456")
    cf.write(open("config_file_path", "w"))
if __name__ == "__main__":
  f = Db_Connector("../conf/db_config.ini")
</div>

得到结果:

通用模块:支持命令行+import两种形式
ini_op.py

#---------------------
import sys,os,time
import ConfigParser


class Config:
    def __init__(self, path):
        self.path = path
        self.cf = ConfigParser.ConfigParser()
        self.cf.read(self.path)
    def get(self, field, key):
        result = ""
        try:
            result = self.cf.get(field, key)
        except:
            result = ""
        return result
    def set(self, filed, key, value):
        try:
            self.cf.set(field, key, value)
            cf.write(open(self.path,'w'))
        except:
            return False
        return True
def read_config(config_file_path, field, key):
    cf = ConfigParser.ConfigParser()
    try:
        cf.read(config_file_path)
        result = cf.get(field, key)
    except:
        sys.exit(1)
    return result

def write_config(config_file_path, field, key, value):
    cf = ConfigParser.ConfigParser()
    try:
        cf.read(config_file_path)
        cf.set(field, key, value)
        cf.write(open(config_file_path,'w'))
    except:
        sys.exit(1)
    return True

if __name__ == "__main__":
   if len(sys.argv) < 4:
      sys.exit(1)

   config_file_path = sys.argv[1]
   field = sys.argv[2]
   key = sys.argv[3]
   if len(sys.argv) == 4:
      print read_config(config_file_path, field, key)
   else:
      value = sys.argv[4]
      write_config(config_file_path, field, key, value)
</div>

第二个示例


def main():
    cp = ConfigParser.ConfigParser()   
    cf = open(u"in.ini")
    cp.readfp(cf)

    secs = cp.sections()
    print cp.sections()

    for sec in secs:
        opts = cp.options(sec)
        for opt in opts:
            val = cp.get(sec, opt)
            val += "test....."
            cp.set(sec, opt, val)

    cp.write(open("out.ini", "w"))

if __name__ == '__main__':
    main()
</div>

</div>

您可能想查找下面的文章:

  • python读写ini配置文件方法实例分析
  • Python读写ini文件的方法
  • Python读写ini文件的方法
  • Python读取ini文件、操作mysql、发送邮件实例
  • Python实现的ini文件操作类分享
  • Python实现的ini文件操作类分享
  • python读写ini文件示例(python读写文件)

相关文章

  • pymongo为mongodb数据库添加索引的方法
  • python使用matplotlib绘制折线图教程
  • Python底层封装实现方法详解
  • 利用Python获取赶集网招聘信息前篇
  • python每隔N秒运行指定函数的方法
  • Python中实现远程调用(RPC、RMI)简单例子
  • 详解Python中time()方法的使用的教程
  • Python实现telnet服务器的方法
  • Python科学计算之NumPy入门教程
  • 浅谈Python的垃圾回收机制

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • django 文件上传功能的相关实例代码(简单易懂)
    • python基于multiprocessing的多进程创建方法
    • 一篇文章入门Python生态系统(Python新手入门指导)
    • 详细解读Python中解析XML数据的方法
    • 解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
    • Python 基于Twisted框架的文件夹网络传输源码
    • python实现从字典中删除元素的方法
    • 使用Python实现下载网易云音乐的高清MV
    • python处理二进制数据的方法
    • Python中使用bidict模块双向字典结构的奇技淫巧

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有