【问题标题】:Errors reading a config file with ConfigParser in python 3,4在 python 3,4 中使用 ConfigParser 读取配置文件时出错
【发布时间】:2015-09-09 02:39:17
【问题描述】:

我在 Python 3.4 中使用 configparser 读取配置集文件时遇到了一个烦人的错误。该代码最初是为 Python 2.7 编写的,但我不再使用它,我只使用 python 3.4。我根本不想要对 python 2.7 的任何向后兼容性。只有且只有 python 3.4

Error reading set file ./sets/diagnostic1.set

上面是我运行此代码时遇到的错误

from configparser import *
import re, os, ctypes, csv, calendar, datetime
from time import *
from ctypes import *

# Reads the config set file 
def readSetFile(file):
   try:
      #config = RawConfigParser()
      #config.readfp(FakeSecHead(open(file)))  #  readfp is deprecated from configparser 
      config = ConfigParser(delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=';',  interpolation=None)
      config.read_file(FakeSecHead(open(file)))
      return config
  except:
    return False


# Reads fake or modified Section head in the config file
class FakeSecHead(object):
  def __init__(self, fp):
      self.fp = fp
      self.sechead = '[main]\n'

  def readline(self):
      if self.sechead:
        try:
            return self.sechead
        finally:
            self.sechead = None
      else:
        line = self.fp.readline()
      return re.sub('^SECTION_3.*$', '[additional]', line)

包含配置的“ diagnostic1.set ”文件

  COMMENTS=Click view comments.
  SECTION_1=################# General Settings #################
  FRAMEWORK_CONFIG=./experts/config/TesingConfig.xml
  USE_ORDER_WRAPPER=0
  UI_FONT_SIZE=12
  UI_ERROR_INFO_COLOR=0
  UI_CUSTOM_INFO_COLOR=0
  SECTION_2=############## Common Strategy Settings ##############
  OPERATIONAL_MODE=1
  STRATEGY_INSTANCE_ID=25
  MAX_DRAWDOWN_PERCENT=100.00000000
  MAX_SPREAD_PIPS=100.00000000
  ENABLE_WFO=0
  WFO_WINDOW_SIZE=0
  PARAMETER_SET_POOL=0.00000000
  DISABLE_COMPOUNDING=0
  USE_INSTANCE_BALANCE=0
  INIT_INSTANCE_BALANCE=0.00000000
  TIMED_EXIT_BARS=31
  ATR_AVERAGING_PERIOD=3
  MAX_OPEN_ORDERS=1
  SECTION_3=############## Additional Strategy Settings ##############
  OPEN_ATR_MULTIPLIER=0.41000000
  OPEN_ATR_MULTIPLIER,F=1
  OPEN_ATR_MULTIPLIER,1=0.20
  OPEN_ATR_MULTIPLIER,2=0.01
  OPEN_ATR_MULTIPLIER,3=0.60
  TRADE_CONFIDENCE=2

所以当我测试在 python 3.4 控制台中运行代码时,我得到“False”,这表明它无法读取配置集文件。

 Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Jun  4 2015, 15:29:08) 
 [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
 Type "help", "copyright", "credits" or "license" for more information.
 >>> from configparser import *
 >>> import re, os, ctypes, csv, calendar, datetime
 >>> from time import *
 >>> from ctypes import *
 >>>
 >>> def readSetFile(file):
 ...     try:
 ...         #config = RawConfigParser()
 ...         #config.readfp(FakeSecHead(open(file)))  #  readfp will be deprecated from configparser edited by developer
 ...         config = ConfigParser(delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=';',  interpolation=None)
 ...         config.read_file(FakeSecHead(open(file)))
 ...         return config
 ...     except:
 ...         return False

 >>> 
 >>> class FakeSecHead(object):
 ...     def __init__(self, fp):
 ...         self.fp = fp
 ...         self.sechead = '[main]\n'
 ...     def readline(self):
 ...         if self.sechead:
 ...             try:
 ...                 return self.sechead
 ...             finally:
 ...                 self.sechead = None
 ...         else:
 ...             line = self.fp.readline()
 ...         return re.sub('^SECTION_3.*$', '[additional]', line)
 ... 


 >>> readSetFile('./sets/diagnostics1.set')
 False
 >>> 

能够按原样读取配置集文件的任何帮助。我有数百个相同格式的配置集文件。但我似乎无法读取 Python 3.4 中的文件

我已经设法从 Patrick MAUPIN 修改了代码 sn-p,它能够读取配置集文件,但现在在代码的同一部分抛出一个关键错误

  numSystemsInPortfolio = len(setFilePaths)
  SettingsArrayType = numSystemsInPortfolio * ctypes.POINTER(c_double)
  settings = SettingsArrayType()
  for i in range(numSystemsInPortfolio):
     settings[i] = SettingsType()
     settings[i][IS_BACKTESTING] = True
     settings[i][DISABLE_COMPOUNDING] = float(sets[i].mainParams["DISABLE_COMPOUNDING"]['value']) if sets[
        i].content.has_option('main', 'DISABLE_COMPOUNDING') else 0
     settings[i][TIMED_EXIT_BARS] = float(sets[i].mainParams["TIMED_EXIT_BARS"]['value']) if sets[
        i].content.has_option('main', 'TIMED_EXIT_BARS') else 0
     settings[i][ORIGINAL_EQUITY] = config.getfloat("account", "balance")
     .....
     .....
     .....
     .....
     .....



    File "mycode.py", line 222, in main
i].content.has_option('main', "DISABLE_COMPOUNDING") else 0
    KeyError: 'DISABLE_COMPOUNDING'

【问题讨论】:

  • 您可以尝试通过在readline() 之后添加print(line) 来调试它的距离。这能告诉你问题出在哪里吗?另外,如果您没有发现错误(在readSetFile 中使用try: except),那么会引发什么错误?你能发布堆栈跟踪吗?
  • 什么是SettingsType?问题似乎在那里。或者mainParams

标签: python python-3.4


【解决方案1】:

除非您的配置文件是兆字节,否则读取它们然后修改它们是最简单的,而不是一次只做一行。此外,StringIO 是将类文件对象传递给任何东西的最佳方式。

下面是一些适用于 Python 3.4 的数据集的代码:

import re
from configparser import ConfigParser
from io import StringIO

# Reads the config set file
def readSetFile(fname):
    with open(fname, 'rb') as f:
        data = f.read().decode('UTF-8')

    cfg_splitter = re.compile('^SECTION_3.*$', re.MULTILINE).split

    cfg_parser = ConfigParser(delimiters=('=', ':'),
                            comment_prefixes=('#', ';'),
                            inline_comment_prefixes=';',
                            interpolation=None)

    data = cfg_splitter(data)
    data.insert(1, '[additional]')
    data.insert(0, '[main]')
    data = '\n'.join(data)
    print(data)
    cfg_parser.read_file(StringIO(data))
    return cfg_parser

config = readSetFile('sets/diagnostics1.set')
for sec in config.sections():
    print(sec, config.items(sec))

print(config.has_option('main', 'DISABLE_COMPOUNDING'))
print(config.has_option('main', 'DISABLE_COMPOUNDING2'))

当我执行它时,我得到:

main [('comments', 'Click view comments.'), ('section_1', '################# General Settings #################'), ('framework_config', './experts/config/TesingConfig.xml'), ('use_order_wrapper', '0'), ('ui_font_size', '12'), ('ui_error_info_color', '0'), ('ui_custom_info_color', '0'), ('section_2', '############## Common Strategy Settings ##############'), ('operational_mode', '1'), ('strategy_instance_id', '25'), ('max_drawdown_percent', '100.00000000'), ('max_spread_pips', '100.00000000'), ('enable_wfo', '0'), ('wfo_window_size', '0'), ('parameter_set_pool', '0.00000000'), ('disable_compounding', '0'), ('use_instance_balance', '0'), ('init_instance_balance', '0.00000000'), ('timed_exit_bars', '31'), ('atr_averaging_period', '3'), ('max_open_orders', '1')]
additional [('open_atr_multiplier', '0.41000000'), ('open_atr_multiplier,f', '1'), ('open_atr_multiplier,1', '0.20'), ('open_atr_multiplier,2', '0.01'), ('open_atr_multiplier,3', '0.60'), ('trade_confidence', '2')]
True
False

【讨论】:

  • 配置非常小,每个大约 25 行 (1.7kB)。嘿帕特里克,我能比以前走得更远。它能够读取配置集文件,但会抛出如上所示的键错误
  • @JourneyMan -- 我认为您没有显示足够的代码来诊断新问题 -- 我已经修改了答案以表明 has_option 适用于配置记录...跨度>
  • 我追踪到部分代码有错误的 (key,value) 映射。您的回答解决了文件读取问题。谢谢
猜你喜欢
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多