導航:首頁 > 編程語言 > python配置文件

python配置文件

發布時間:2022-02-16 14:56:10

1. 如何使用python3讀取配置文件

  1. 如果你的配置文件是文本文件,那個只能通過open去讀取,獲取文件裡面的內容。

  2. 如果是py文件,可以直接importpy文件讀取

2. 怎麼在python中創建配置文件

沒辦法設置默認編輯器,因為可能和其他的無後綴文件沖突。你可以這樣右鍵->open
with..->Other..
在裡面找到你的python編輯器。

3. python 怎麼讀取配置文件

python 讀取配置文件方法

#coding=utf8

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open(raw_input("input file name:"), "rb"))
print config.get("global", "ip")


config.ini
[global]
ip = 192.168.1.100 ;ip地址
port = 3306

4. 如何用python來修改配置文件conf

樓上的倆人回答綜合一下就是完美的答案,文件打開方式file.open。但是這種方式比較low,但是如果就是一簡單的讀寫文件用著方便,configparser是專門的conf庫,有一點點(只是一點點)學習成本,但是也很方便。推薦後者。

5. 如何使用Python3讀寫INI配置文件

import configparser
# 生成config對象
conf =configparser.ConfigParser()
# 用config對象讀取配置文件
conf.read("xxx.ini")
#從ini配置文件中獲取信息
a=conf.get("xx","yy")
print(a) 結果為: zz
xxx.ini
[xx]
yy=zz

6. python 如何實現配置文件中的配置項加密

可以在寫入配置文件的時候,進行加密,讀取配置後解密即可

比如使用base64加密:
base64.b64encode加密,base64.b64decode解密

7. 如何創建python的cfg配置文件

讀取config中info段中的name變數值.最後講講如何設置值.使用set(段名,變數名,值) 來設置變數.config.set(''info'',''age'',''21'') 表示把info段中age變數設置為21. 就這么簡單.

8. python3 如何創建一個.ini的配置文件。

1、說明:
python3使用configparser模塊來處理ini配置文件。

2、代碼示例:
需要生成conf.ini配置文件如下:
[config]
v1 = 100
v2 = abc
v3 = true
v4 = 123.45

python代碼:
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的所有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將內容寫入配置文件。

9. python修改配置文件不成功

你支持從」新建文本文檔.cfg「讀取了配置,並沒有更新它,所以當然不會改變了。
參考下:
cfgfile = open("c:\\next.ini",'w')
# add the settings to the structure of the file, and lets write it out...
Config.add_section('Person')
Config.set('Person','HasEyes',True)
Config.set('Person','Age', 50)
Config.write(cfgfile)
cfgfile.close()

閱讀全文

與python配置文件相關的資料

熱點內容
文件如何解壓再壓縮 瀏覽:620
使用編譯命令導入庫 瀏覽:116
app一般用什麼伺服器 瀏覽:819
手機端java編譯 瀏覽:145
電腦文件夾一右鍵就卡 瀏覽:194
javalistforeach 瀏覽:26
奇葩解壓筆 瀏覽:333
php判斷字元串開頭 瀏覽:268
多個pdf合並成一個pdf 瀏覽:733
linuxc獲取mac 瀏覽:964
linux線程運行時間 瀏覽:379
機器人逆運算演算法 瀏覽:302
腰椎壓縮性骨折屬於幾級傷殘 瀏覽:827
phonegapjavajs 瀏覽:327
設計一個簡單遺傳演算法 瀏覽:177
單片機的直流電機的pwm控制 瀏覽:448
cc在線編譯教程 瀏覽:373
Linux伺服器如何安裝win 瀏覽:591
golang演算法圖 瀏覽:698
ARM單片機開發工具 瀏覽:84