繁体   English   中英

如何读取 python 中的属性文件

[英]How to read properties file in python

我有一个名为Configuration.properties的属性文件,其中包含:

path=/usr/bin
db=mysql
data_path=/temp

我需要阅读此文件并在后续脚本中使用pathdbdata_path等变量。 我可以使用 configParser 还是简单地读取文件并获取值来执行此操作。

提前致谢。

对于没有节头的配置文件,用[]包围 - 你会发现抛出ConfigParser.NoSectionError异常。 通过插入“假”部分标题可以解决此问题- 如本答案所示

如果文件很简单,如pcalcao 的回答中所述,您可以执行一些字符串操作来提取值。

这是一个代码片段,它返回配置文件中每个元素的键值对字典。

separator = "="
keys = {}

# I named your file conf and stored it 
# in the same directory as the script

with open('conf') as f:

    for line in f:
        if separator in line:

            # Find the name and value by splitting the string
            name, value = line.split(separator, 1)

            # Assign key value pair to dict
            # strip() removes white space from the ends of strings
            keys[name.strip()] = value.strip()

print(keys)

如果您需要以简单的方式从属性文件中的某个部分读取所有值:

您的config.properties文件布局:

[SECTION_NAME]  
key1 = value1  
key2 = value2  

你编码:

   import configparser

   config = configparser.RawConfigParser()
   config.read('path_to_config.properties file')

   details_dict = dict(config.items('SECTION_NAME'))

这将为您提供一个字典,其中的键与配置文件中的键及其对应的值相同。

details_dict是:

{'key1':'value1', 'key2':'value2'}

现在获取 key1 的值: details_dict['key1']

将所有内容放在一个方法中,该方法仅从配置文件中读取该部分一次(在程序运行期间第一次调用该方法)。

def get_config_dict():
    if not hasattr(get_config_dict, 'config_dict'):
        get_config_dict.config_dict = dict(config.items('SECTION_NAME'))
    return get_config_dict.config_dict

现在调用上面的函数并获取所需的键值:

config_details = get_config_dict()
key_1_value = config_details['key1'] 

-------------------------------------------------- -----------

扩展上述方法,自动逐节读取,然后按节名和键名访问。

def get_config_section():
    if not hasattr(get_config_section, 'section_dict'):
        get_config_section.section_dict = dict()

        for section in config.sections():
            get_config_section.section_dict[section] = 
                             dict(config.items(section))

    return get_config_section.section_dict

访问:

config_dict = get_config_section()

port = config_dict['DB']['port'] 

(这里'DB'是配置文件中的一个部分名称,'port'是'DB'部分下的一个键。)

我喜欢当前的答案。 而且......我觉得在“真实世界”中有一种更清洁的方法。 如果您正在执行任何规模或规模的项目,尤其是在“多”环境领域,则必须使用节标题功能。 我想把它和格式良好的可复制代码放在一起,使用一个健壮的现实世界的例子。 这在 Ubuntu 14 中运行,但跨平台工作:

真实世界的简单示例


设置示例(终端):

cd ~/my/cool/project touch local.properties touch environ.properties ls -la ~/my/cool/project -rwx------ 1 www-data www-data 0 Jan 24 23:37 local.properties -rwx------ 1 www-data www-data 0 Jan 24 23:37environ.properties

设置好权限

>> chmod 644 local.properties
>> chmod 644 env.properties
>> ls -la
-rwxr--r-- 1 www-data www-data  0 Jan 24 23:37 local.properties
-rwxr--r-- 1 www-data www-data  0 Jan 24 23:37 environ.properties

编辑您的属性文件。

文件 1:local.properties

[global]
relPath=local/path/to/images              
filefilters=(.jpg)|(.png)

[dev.mysql]
dbPwd=localpwd
dbUser=localrootuser

[prod.mysql]
dbPwd=5tR0ngpwD!@#
dbUser=serverRootUser

[branch]
# change this to point the script at a specific environment
env=dev

文件 2:environ.properties

#----------------------------------------------------
# Dev Environment
#----------------------------------------------------

[dev.mysql]
dbUrl=localhost
dbName=db

[dev.ftp]
site=localhost
uploaddir=http://localhost/www/public/images

[dev.cdn]
url=http://localhost/cdn/www/images


#----------------------------------------------------
# Prod Environment
#----------------------------------------------------
[prod.mysql]
dbUrl=http://yoursite.com:80
dbName=db

[prod.ftp]
site=ftp.yoursite.com:22
uploaddir=/www/public/

[prod.cdn]
url=http://s3.amazon.com/your/images/

Python文件:readCfg.py

import ConfigParser import osimport ConfigParser import os

# a simple function to read an array of configuration files into a config object
def read_config(cfg_files):
    if(cfg_files != None):
        config = ConfigParser.RawConfigParser()

        # merges all files into a single config
        for i, cfg_file in enumerate(cfg_files):
            if(os.path.exists(cfg_file)):
                config.read(cfg_file)

        return config

Python 文件:yourCoolProgram.py

from readCfg import read_config

#merge all into one config dictionary
config      = read_config(['local.properties', 'environ.properties'])

if(config == None):
    return

# get the current branch (from local.properties)
env      = config.get('branch','env')        

# proceed to point everything at the 'branched' resources
dbUrl       = config.get(env+'.mysql','dbUrl')
dbUser      = config.get(env+'.mysql','dbUser')
dbPwd       = config.get(env+'.mysql','dbPwd')
dbName      = config.get(env+'.mysql','dbName')

# global values
relPath = config.get('global','relPath')
filefilterList = config.get('global','filefilters').split('|')

print "files are: ", fileFilterList, "relative dir is: ", relPath
print "branch is: ", env, " sensitive data: ", dbUser, dbPwd

结论

鉴于上述配置,您现在可以拥有一个通过更改“local.properties”中的 [branch]env 值来完全改变环境的脚本。 而这一切都是基于良好的配置原则! 耶!

是的,是的,你可以。

ConfigParser ( https://docs.python.org/2/library/configparser.html ) 将为您提供一个很好的小结构来从开箱即用中获取值,手动操作将需要一些字符串拆分,但对于一个简单的格式文件,没什么大不了的。

问题是“我如何阅读这个文件?”。

一个用于读取非结构化属性文件(无节)而忽略注释的衬垫:

with open(props_file_path, "r", encoding="utf-8") as f:
    props = {e[0]: e[1] for e in [line.split('#')[0].strip().split('=') for line in f] if len(e) == 2}

保持简单。 创建名称=值对的属性文件并将其保存为 filename.py。 然后您所要做的就是导入它并将属性引用为 name.value。 就是这样...

我的 Oracle 连接的配置文件名为 config.py,如下所示。

username = "username"
password = "password"
dsn = "localhost/xepdb1"
port = 1521
encoding = "UTF-8"

所以,在 Python 中,我所要做的就是......

import cx_Oracle
import config

conn = None
cursor = None

try:

    print("connecting...");

    conn = cx_Oracle.connect(config.username, config.password, config.dsn, encoding=config.encoding)

    cursor = conn.cursor()

    print("executing...");

    # don't put a ';' at the end of SQL statements!
    cursor.execute("SELECT * FROM employees")

    print("dumping...");

    for row in cursor:
        print(row[0])

except cx_Oracle.Error as error:
    print(error)
finally:
    print("finally...")
    if cursor:
        cursor.close()
    if conn:
        conn.close()

如果你想在 python 中读取一个 proeprties 文件,我的第一个建议,我自己没有遵循,因为我太喜欢 Visual Code ......

在 Jython 上运行你的 python。 一旦您在 Jython 上运行 python,您就可以轻松地打开一个 java util 输入流到您的数据文件。 使用 java.utl.Properties 可以调用 load() api,然后就可以开始了。 所以我的建议是,做最简单的事情,开始使用 java 运行时环境和 jython。

顺便说一句,我当然是使用 jython 来运行 python 的。 对此没有任何疑问。

但是我没有做的是使用 jython 来调试 python ......可悲的是! 对我来说问题是我使用 microsft 可视化代码来编写 pythong,然后是的......然后我被困在我的正常 python 安装中。 不是理想的世界!

如果这就是你的情况。 然后你可以去计划(b)......尽量不使用JDK库并在其他地方找到替代方案。

所以这里是sugestion。 这是我发现的一个库,我正在使用它来读取属性文件。 https://pypi.python.org/pypi/jproperties/1.0.1#downloads

 from jproperties import Properties with open("foobar.properties", "r+b") as f: p = Properties() p.load(f, "utf-8") # Do stuff with the p object... f.truncate(0) p.store(f, encoding="utf-8")

所以在上面的代码引用中,你看到了他们如何打开属性文件。 擦除它,然后再次将属性写回到文件中。

您将属性对象视为字典对象。 然后你做这样的事情:

myPropertiesTuple = propertiesObjec[myPropertyKey]

当心。 当您使用上述 api 并获取键的值时,该值是一个 PropertiesTuple。 它是一对(值,元数据)。 因此,您要寻找的值可以从 myPropertiesTuple[0] 中获取。 除此之外,只需阅读该库运行良好的页面上的文档。

我正在使用方法(b)。 如果在某些时候使用 java 库的优势超过了坚持使用原生 python 语言的优势,以便我可以调试可视化代码。

我将听到对纯 python 运行时的节拍支持,并使用硬依赖项 jython 运行时/java 库对代码进行妥协。 到目前为止还没有必要。

那么,蓝色账单还是红色药丸?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM