python ConfigParser讀取設定檔,及解決報錯ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

來源:互聯網
上載者:User

標籤:圖片   call   getopt   value   python   字串   return   raise   Null 字元串   

先說一下在讀取設定檔時報錯的問題--ConfigParser.MissingSectionHeaderError: File contains no section headers

問題描述:

在練習ConfigParser讀取設定檔時,cmd一直報一個錯:ConfigParser.MissingSectionHeaderError: File contains no section headers.

D:\test_python>python task_test.py
Traceback (most recent call last):
  File "task_test.py", line 20, in <module>
    pp=ParsePageObjectRepositoryConfig()
  File "task_test.py", line 9, in __init__
    self.cf.read("D:\\test_python\\dataDriven\\conf\\PageObjectRepository.ini")
  File "C:\Python27\lib\ConfigParser.py", line 305, in read
    self._read(fp, filename)
  File "C:\Python27\lib\ConfigParser.py", line 512, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: D:\test_python\dataDriven\conf\PageObjectRepository.ini, line: 1
\xef\xbb\xbf#\xe6\xaf\x8f\xe4\xb8\xaa\xe9\xa1\xb5\xe9\x9d\xa2\xe7\x94\xa8\xe4\xb8\x80\xe4\xb8\xaasection\xe6\xa0\x87\xe8\xaf\x86\n‘

百度了一下網上的解決方案,

報錯是因為設定檔PageObjectRepository.ini在windows下經過notepad編輯後儲存為UTF-8或者unicode格式的話,會在檔案的開頭加上兩個位元組“\xff\xfe”或者三個位元組“\xef\xbb\xbf”。 就是--》BOM

解決的辦法就是在設定檔被讀取前,把DOM位元組個去掉。

網上也給了一個用正則去掉BOM位元組的函數:就是把對應的位元組替換成Null 字元串

remove_BOM()函數定義:

def remove_BOM(config_path):
    content = open(config_path).read()
    content = re.sub(r"\xfe\xff","", content)
    content = re.sub(r"\xff\xfe","", content)
    content = re.sub(r"\xef\xbb\xbf","", content)
    open(config_path, ‘w‘).write(content)

下面貼一下我的設定檔和讀取設定檔的代碼--:

 

代碼:

#encoding=utf-8
from ConfigParser import ConfigParser
import re

def remove_BOM(config_path):#去掉設定檔開頭的BOM位元組
    content = open(config_path).read()
    content = re.sub(r"\xfe\xff","", content)
    content = re.sub(r"\xff\xfe","", content)
    content = re.sub(r"\xef\xbb\xbf","", content)
    open(config_path, ‘w‘).write(content)

class ParsePageObjectRepositoryConfig(object):
    def __init__(self,config_path):
        self.cf=ConfigParser()#產生解析器
        self.cf.read(config_path)
        print "-"*80
        print "cf.read(config_path):\n", self.cf.read(config_path)

    def getItemsFromSection(self,sectionName):
        print self.cf.items(sectionName)
        return dict(self.cf.items(sectionName))

    def getOptionValue(self,sectionName,optionName):#返回一個字典
        return self.cf.get(sectionName,optionName)

 

if __name__==‘__main__‘:
    remove_BOM("D:\\test_python\\PageObjectRepository.ini")
    pp=ParsePageObjectRepositoryConfig("D:\\test_python\\PageObjectRepository.ini")
    remove_BOM
    print "-"*80
    print "items of ‘126mail_login‘:\n",pp.getItemsFromSection("126mail_login")
    print "-"*80
    print "value of ‘login_page.username‘ under section ‘126mail_login‘:\n",pp.getOptionValue("126mail_login","login_page.username")

 

結果:

D:\test_python>python task_test.py
--------------------------------------------------------------------------------
cf.read(config_path):
[‘D:\\test_python\\PageObjectRepository.ini‘]
--------------------------------------------------------------------------------
items of ‘126mail_login‘:
[(‘login_page.frame‘, ‘id>x-URS-iframe‘), (‘login_page.username‘, "xpath>//input[@name=‘email‘]"), (‘login_page.password‘, "xpath>//input[@name=‘password‘]"), (‘login_page.loginbutton‘, ‘id>dologin‘)]
{‘login_page.loginbutton‘: ‘id>dologin‘, ‘login_page.username‘: "xpath>//input[@name=‘email‘]", ‘login_page.frame‘: ‘id>x-URS-iframe‘, ‘login_page.password‘: "xpath>//input[@name=‘password‘]"}
--------------------------------------------------------------------------------
value of ‘login_page.username‘ under section ‘126mail_login‘:
xpath>//input[@name=‘email‘]

 

python ConfigParser讀取設定檔,及解決報錯ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.