如何从Python Config文件解析多参数函数?

时间:2017-07-10 01:24:06

标签: python config

我正在尝试在Python配置文件中使用多参数功能。当我尝试将其调用到脚本然后将参数转换为int时出现问题。有没有办法在python中执行此操作?

#Config File-----------------------------------

[Camera_Settings]
SENSOR-TAP = 1
ROI = 640,320,8,6

#Python Script--------------------------------

#! /usr/bin/python

import configparser
import serial

List_of_Camera_Commands = []            

# Establish serial port
'''
ser = serial.Serial('/dev/ttyUSB0')
print(ser.name)
ser.baudrate = 192000
ser.write(stuff to write)
ser.read()
'''

CameraConfig = configparser.ConfigParser()
CameraConfig.read("CameraConfig.conf")        #Read in Config File

#---------Code to read in Config file Options and cast as   ints---------------


for name in CameraConfig.options('Camera_Settings'):                            
Casted_Int_Values = CameraConfig.getint('Camera_Settings', name)           

print(name, ": ",Casted_Int_Values)                                         

Temp_command = [name,  Casted_Int_Values]                                   

List_of_Camera_Commands.append(Temp_command)                              

print(List_of_Camera_Commands)                                                 

#ser.write(List_of_Camera_Commands)       ------> write to serial????







#output = invalid literal for int() with base 10: '640,320,8,6

1 个答案:

答案 0 :(得分:0)

ROI变量不是int,而是包含多个int的字符串。你可能会得到第一个。

尝试在多行上分割ROI。或者期望ROI是一个可以在逗号上拆分的字符串,然后在int()上用于转换为int。 :)

尝试在for循环之前执行此操作,以便在将其作为int之前查看值。

for name in CameraConfig.options('Camera_Settings'):
    print(name, CameraConfig.get('Camera_Settings', name))