Python 在.INI文件中将变量值更改为浮动

Python 在.INI文件中将变量值更改为浮动,python,python-2.7,Python,Python 2.7,我使用一个.INI文件将变量从Python来回传递到使用BASIC的软件,在分析数据后,我想更新某些变量的值 这是我当前的代码: import numpy as np try: from configparser import ConfigParser except ImportError: from ConfigParser import ConfigParser config = ConfigParser() config.read('ParameterFile.ini'

我使用一个.INI文件将变量从Python来回传递到使用BASIC的软件,在分析数据后,我想更新某些变量的值

这是我当前的代码:

import numpy as np

try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser

config = ConfigParser()
config.read('ParameterFile.ini')

# read values from a section
GainAff = config.getfloat('Section 1', 'gainaff')
GainVff = config.getfloat('Section 1', 'gainvff')
FeedforwardAdvance = config.getfloat('Section 1', 'feedforwardadvance')
TrajectoryFIRFilter = config.getfloat('Section 1', 'trajectoryfirfilter')
RampRate = config.getfloat('Section 1', 'ramprate')

print 'GainAff = ', GainAff
print 'GainVff = ', GainVff
print 'Feedforward Advance = ', FeedforwardAdvance
print 'Trajectory FIR Filter = ', TrajectoryFIRFilter
print 'Ramp Rate = ', RampRate

new_GainAff = 1.0
new_GainVff = 2.0
new_FeedforwardAdvance = 0.3
new_TrajectoryFIRFilter = 0.5
new_RampRate = 4000

##update existing value
config.set('Section 1', 'GainAff', new_GainAff)
config.set('Section 1', 'GainVff', new_GainVff)
config.set('Section 1', 'FeedforwardAdvance', new_FeedforwardAdvance)
config.set('Section 1', 'TrajectoryFIRFilter', new_TrajectoryFIRFilter)
config.set('Section 1', 'RampRate', new_RampRate)

##save to a file
with open('ParameterUpdate.ini', 'w') as configfile:
    config.write(configfile)
    ## Write some stuff in here
configfile.close()
但是,当我尝试使用
config.set
设置新变量时,我被告知不能使用
float
值:

  File "/home/pi/.local/lib/python2.7/site-packages/backports/configparser/__init__.py", line 1221, in _validate_value_types
    raise TypeError("option values must be strings")
TypeError: option values must be strings
在查看这篇文章时,我看到许多人使用带有浮点数的
config.set
,我不确定这是否是因为python版本等原因(我使用的是python 2.7.13)


我做错了什么?

设置函数需要一个字符串: 使用以下代码强制转换为字符串:

config.set('Section 1', 'GainAff', str(new_GainAff))
从我在这里读到的内容来看,python 3似乎也是这样:

我们可以确认Python3也是如此。以下是我的python 3解释器的输出:

>>> config = ConfigParser()
>>>
>>> config.read('b.ini')
['b.ini']
>>> config.set('Section 1', 'GainAff', 1.2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\PYTHON34\LIB\configparser.py", line 1165, in set
    self._validate_value_types(option=option, value=value)
  File "C:\PYTHON34\LIB\configparser.py", line 1154, in _validate_value_types
    raise TypeError("option values must be strings")
TypeError: option values must be strings
>config=ConfigParser()
>>>
>>>config.read('b.ini')
['b.ini']
>>>config.set('Section 1','GainAff',1.2)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\PYTHON34\LIB\configparser.py”,第1165行,在集合中
自我验证值类型(选项=选项,值=值)
文件“C:\PYTHON34\LIB\configparser.py”,第1154行,在_validate\u value\u类型中
raise TypeError(“选项值必须是字符串”)
TypeError:选项值必须是字符串
说明:虽然可以使用RawConfigParser(或原始参数设置为true的ConfigParser)进行非字符串值的内部存储,但只有使用字符串值才能实现完整功能(包括插值和文件输出)。