1.引入from config import configuration
2.使用
1>zw_group_config = configuration("zw_group_import.json").get_all() #json型別
2>oracle_con = configuration("database.toml").get_all()['oracle']['oracle_url'] # toml型別
result = oracle_con.query_all_dict_generator(zw_group_config["sql"])
[json] #json樣例
[toml] #toml樣例
[oracle]
oracle_uri = "user_name/password@ip:port/orcl" #使用者名稱/密碼@主機ip:埠/服務名
# 引用檔案 config.py
# -*- coding: utf-8 -*-
import json
import yaml
import toml
from six import text_type, py3
decoder =
class configuration(object):
def __init__(self, file_name):
self.file_name = file_name
extension = file_name.rsplit('.', 1)[-1] # 右邊開始最後乙個.進行分割一次 生成列表後 取列表最後乙個值
loader = decoder.get(extension, json)
try:
if loader == json and py3: # pragma: no cover
fh = open(file_name, encoding='utf8')
# elif loader == yaml:
# fh = open(file_name, 'rb')
elif loader == toml and py3:
fh = open(file_name, encoding='utf-8-sig')
else:
fh = open(file_name)
try:
self.data = loader.load(fh)
except valueerror as ve:
print("invalid freeze file: %s" % ve)
fh.close()
except ioerror as ioe:
print(text_type(ioe))
def get_all(self):
return self.data
def get_one(self, keyword, dict_a=none):
if dict_a is none:
dict_a = self.data
for temp_key, temp_value in dict_a.items():
if temp_key == keyword:
return temp_value
if isinstance(temp_value, dict):
value = self.get_one(keyword, temp_value)
if value is not none:
return value
return none
讀取配置檔案
類載入器 1 直接用class.forname 類的路徑名 class.forname com.hsf.classloader.classloader 2 用classloder 類載入器 inputstream in classloader.getsystemresourceasstream aa...
讀取配置檔案並讀取檔案
配置檔案 url users z ten documents name myfile.txt url是讀寫檔案的檔案位址,name是讀寫檔案的檔名稱 fileinputstream is null try catch filenotfoundexception e2 properties pt ne...
C 讀取配置檔案
1 首先引入標頭檔案 include 2 獲取應用程式的當前路徑 char buf 1000 getcurrentdirectory 1000,buf 得到當前工作路徑3 獲取配置檔案的路徑 char path 1024 definesysconfig config.ini sprintf path...