從ini文件解析錯誤的值,我如何解析用戶名= username而不是值從值?Python - 如何解決從ini文件中解析正確的值,但不解析值字段中的值
1)存儲與預定義的預設,這是我需要在Python閱讀
$ cat /var/tmp/file.ini
banner=QUESTIONS sentence
network=lan
programming=pytohn
url=http\://pbx/register?username=E300B1&password=1234&option=localip&localip=
username=E300B1
password=1234
server=pbx
2)代碼 INI文件:我試圖似乎是錯誤的用戶名/密碼字段
import re,os, time, socket, datetime, threading, subprocess, logging, gtk, gobject
logging.basicConfig(filename='/var/tmp/log.log',level=logging.DEBUG)
def readini(findme):
f = open('/var/tmp/file.ini', "r")
for line in f:
if line:
if findme in line:
r= line.split("=")
return r[1].replace("\\n", "").rstrip()
host = readini("server")
username = preadini("username")
password = readini("password")
command = """curl 'http://%s/a/b?username=%s&password=%s&language=EN'""" % (host, username, password)
logging.debug(command)
os.system(command)
3)輸出(錯誤):
DEBUG:root:curl 'http://192.168.1.10/a/b?username=http\://pbx/register?username&password=http\://pbx/register?username&language=EN'
4)預計產量爲:
DEBUG:root:curl 'http://192.168.1.10/a/b?username=E300B1&password=1234&language=EN'
假設你在控制配置文件格式,有更好的選擇可用:或者嘗試標準的python模塊ConfigParser,或者(我最喜歡的解決方案)使用YAML格式的配置文件並通過'import yaml'將其讀入字典中。通過'cfg = yaml.load(open(「config.yaml」))' –