Python 如何从控制器/主代码中提取冗长的配置

Python 如何从控制器/主代码中提取冗长的配置,python,Python,在我的controller.py文件中,我有很多操作要做 我还需要以下字典格式的配置 但是这些配置,参数模板,头。。。。你真的让我分心 如何将它们保存在另一个Python文件中,并将它们加载到当前的controller.py 谢谢 params_template=""" { "__EVENTTARGET": "AvailabilitySearchInputSearchView$LinkButtonSubmit", "availabilitySearch.SearchIn

在我的
controller.py
文件中,我有很多操作要做

我还需要以下字典格式的配置

但是这些配置,
参数模板
。。。。你真的让我分心

如何将它们保存在另一个Python文件中,并将它们加载到当前的
controller.py

谢谢

  params_template="""
   {
    "__EVENTTARGET": "AvailabilitySearchInputSearchView$LinkButtonSubmit",
    "availabilitySearch.SearchInfo.SalesCode": null,
    "availabilitySearch.SearchInfo.SearchStations[0].DepartureStationCode": FROM_CITY,
    "availabilitySearch.SearchInfo.SearchStations[0].ArrivalStationCode": TO_CITY,
    "availabilitySearch.SearchInfo.SearchStations[0].DepartureDate": "11/28/2015",
    "availabilitySearch.SearchInfo.SearchStations[1].DepartureStationCode": null,
    "availabilitySearch.SearchInfo.SearchStations[1].ArrivalStationCode": null,
    "availabilitySearch.SearchInfo.SearchStations[1].DepartureDate": null,
    "availabilitySearch.SearchInfo.Direction": "Oneway",
    "fromDate": "1448640000000",
    "returnDate": null,
    "fromDate_1": null,
    "fromDate_2": null,
    "availabilitySearch.SearchInfo.AdultCount": "1",
    "availabilitySearch.SearchInfo.ChildrenCount": "0",
    "availabilitySearch.SearchInfo.InfantCount": "0",
    "availabilitySearch.SearchInfo.PromoCode": null
  }
  """

  headers = {
      'Origin': 'http://www.flyscoot.com',
      'Accept-Encoding': 'gzip, deflate',
      'Accept-Language': 'en-US,en;q=0.8',
      'Upgrade-Insecure-Requests': '1',
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
      'Cache-Control': 'max-age=0',
      'Referer': 'http://www.flyscoot.com/index.php/en/',
      'Connection': 'keep-alive',
      'AlexaToolbar-ALX_NS_PH': 'AlexaToolbar/alxg-3.3',
  }

  ...

您可以从您拥有的数据中创建一个单独的配置文件。例如 在文件
test.ini
中,按如下方式存储数据:

[headers]
Origin: 'http://www.flyscoot.com'
Accept-Encoding: 'gzip, deflate'
Accept-Language: 'en-US,en;q=0.8'
Upgrade-Insecure-Requests: '1'
User-Agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36'
Content-Type: 'application/x-www-form-urlencoded'
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
Cache-Control: 'max-age=0'
Referer: 'http://www.flyscoot.com/index.php/en/'
Connection: 'keep-alive'
AlexaToolbar-ALX_NS_PH: 'AlexaToolbar/alxg-3.3' 
完成后,现在可以使用来实现对数据的非常灵活的控制

如果将以下代码与上述数据一起应用,请查看您可以实现的功能:

from ConfigParser import RawConfigParser

parser= RawConfigParser()
parser.read('demo.ini')

print 
#To retrieve all the sections 
print parser.sections()

print
#to retrieve options of a section
print parser.options('headers')

#To get value of each option in a section
for i in parser.options('headers'):
     print parser.get('headers',i) 
输出:

['headers']

['origin', 'accept-encoding', 'accept-language', 'upgrade-insecure-requests', 'user-agent', 'content-type', 'accept', 'cache-control', 'referer', 'connection', 'alexatoolbar-alx_ns_ph']

'http://www.flyscoot.com'
'gzip, deflate'
'en-US,en;q=0.8'
'1'
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36'
'application/x-www-form-urlencoded'
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
'max-age=0'
'http://www.flyscoot.com/index.php/en/'
'keep-alive'
'AlexaToolbar/alxg-3.3'
从中阅读有关ConfigParser的更多信息。希望这有帮助:)