简体   繁体   中英

Changing Variable Values to Float in .INI File

I am using an .INI file to pass variables back and forth from Python to software using BASIC, and upon analyzing data, I want to update the values of certain variables.

This is my current code:

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()

However, when I try to set my new variables using config.set , I am told I can't use a float value:

  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

Upon looking this up, I see many people use config.set with floats, and I am unsure about whether it because of the version of python, etc. (I am using Python 2.7.13)

What am I doing wrong?

The set function requires a string: https://docs.python.org/2/library/configparser.html Cast to a string using code like:

config.set('Section 1', 'GainAff', str(new_GainAff))

From what I read here, this also appears to be the case for python 3: https://docs.python.org/3/library/configparser.html

Can confirm that this is also the case for python 3. The following is output from my python 3 interpreter:

>>> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM