我有一个脚本,可以将一些数据保存在excel文件中并进行排序,并为一些关键内容添加颜色。 我使用的是外部.ini文件,因为有时需要根据当天的用户需求进行更改
ini文件基本上是这样的
[section]
#Color 1
color01 = ('00FCC84E')
cat1 = ('Item1','Item2')
#Color 2
color02 = ('00F4426E')
cat2 = ('Thingy Size 5/16')
我的使用Config Parser的脚本部分执行此操作
import configparser
from configparser import ConfigParser
from ast import literal_eval
config = ConfigParser()
config.read("MyFile.ini")
config.sections()
def variables(section):
dict1 = {}
options = config.options(section)
for option in options:
try:
dict1[option] = config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
color01V = literal_eval(config['ConfigFile']['color01'])
color02V = literal_eval(config['ConfigFile']['color02'])
cat01V = literal_eval(config['ConfigFile']['cat1'])
cat02V = literal_eval(config['ConfigFile']['cat2'])
print(cat01V)
print(cat02V)
这将返回
('Item1','Item2')
Thingy Size 5/16
为什么第二个字符串不带()返回,我该如何解决。
我实际上需要稍后在使用变量时显示()
答案 0 :(得分:0)
您是否尝试过将“ cat1”的值放在引号中?
[section]
color01 = ('00FCC84E')
cat1 = "('Item1','Item2')"
...
cat2 = "('Thingy Size 5/16')"