配置解析器从 ini 文件中获取它们后如何将 "list of words" 变成变量
how to turn a "list of words" into a variable after config parser grabs them from an ini file
我有一个脚本,它使用配置解析器和一个 ini 文件来获取列表
使用它是因为最终用户将放入他或她自己的列表以用于排序脚本
config.ini 文件的一部分示例
[ConfigFile]
#L1 Category to Keep
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
我的脚本中有这个来抓取 ini 文件
import configparser
from configparser import ConfigParser
config = ConfigParser()
config.read("SortingConfig.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
我遇到的问题部分....
正在转动
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
转化为可用的变量值
进一步进入我的脚本我有这个
xls_book = xlrd.open_workbook(infilepath, formatting_info = True, encoding_override = "cp1252", logfile = open(os.devnull, 'w'))
sheetname = xls_book.sheet_by_index(0).name
df = pd.read_excel(xls_book, engine = 'xlrd',parse_dates = ['Date'])
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%y %H:%M:%S').dt.strftime('%m/%d/%y %H:%M')
## L1 Category keep if isin list
df = df.loc[df['L1 Category'].isin(l1cat)]
我得到的错误是
TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]
在最终用户需要对其进行动态更改之前,我可以直接将 l1cat = ('stuff','more stuff','even more stuff') 添加为常规变量,并且效果很好
我使用配置解析器的原因是因为当我编译为 EXE 时,我需要一些可以通过配置文件随时更改变量的东西
我实际上需要它来工作就像我已经将 l1cat = ('stuff','more stuff','even more stuff')
直接输入到我的脚本中一样
固定文件看起来像这样,仍然使用与上面相同的 ini 示例
import configparser
from configparser import ConfigParser
from ast import literal_eval
config = ConfigParser()
config.read("SortingConfig.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
#The Variable gets Defined
l1catV = literal_eval(config['ConfigFile']['l1cat'])
###Further down in my script as I need to use the variable
xls_book = xlrd.open_workbook(infilepath, formatting_info = True, encoding_override = "cp1252", logfile = open(os.devnull, 'w'))
sheetname = xls_book.sheet_by_index(0).name
df = pd.read_excel(xls_book, engine = 'xlrd',parse_dates = ['Date'])
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%y %H:%M:%S').dt.strftime('%m/%d/%y %H:%M')
## L1 Category keep if isin list
##Variable gets used
df = df.loc[df['L1 Category'].isin(l1catV)]
您可以使用 ast.literal_eval
(doc) 来解析值:
txt = '''
[ConfigFile]
#L1 Category to Keep
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
'''
import configparser
from configparser import ConfigParser
from ast import literal_eval
config = ConfigParser()
#####config.read_string(txt)###### - This portion was incorrect
my_tuple = literal_eval(config['ConfigFile']['l1cat'])
for val in my_tuple:
print(val)
打印:
Audio Terminal
Communications/Telephone
Microphones
Speakers
Tour Sound
我似乎可以正常工作...除了部分解析返回了错误的变量,而且我不确定我遗漏了什么
在我的 config.ini
[section]
#Color 1
color01 = ('00FCC84E')
cat1 = ('Speakers','Atlas Mic Stands')
#Color 2
color02 = ('00F4426E')
cat2 = ('Audio Mixers/Console')
当我使用
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)
一个returns有(),另一个没有....
我需要做什么才能让两者都返回 ()
('Speakers', 'Atlas Mic Stands')
Audio Mixers/Console
我有一个脚本,它使用配置解析器和一个 ini 文件来获取列表 使用它是因为最终用户将放入他或她自己的列表以用于排序脚本
config.ini 文件的一部分示例
[ConfigFile]
#L1 Category to Keep
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
我的脚本中有这个来抓取 ini 文件
import configparser
from configparser import ConfigParser
config = ConfigParser()
config.read("SortingConfig.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
我遇到的问题部分.... 正在转动
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
转化为可用的变量值
进一步进入我的脚本我有这个
xls_book = xlrd.open_workbook(infilepath, formatting_info = True, encoding_override = "cp1252", logfile = open(os.devnull, 'w'))
sheetname = xls_book.sheet_by_index(0).name
df = pd.read_excel(xls_book, engine = 'xlrd',parse_dates = ['Date'])
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%y %H:%M:%S').dt.strftime('%m/%d/%y %H:%M')
## L1 Category keep if isin list
df = df.loc[df['L1 Category'].isin(l1cat)]
我得到的错误是 TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]
在最终用户需要对其进行动态更改之前,我可以直接将 l1cat = ('stuff','more stuff','even more stuff') 添加为常规变量,并且效果很好
我使用配置解析器的原因是因为当我编译为 EXE 时,我需要一些可以通过配置文件随时更改变量的东西
我实际上需要它来工作就像我已经将 l1cat = ('stuff','more stuff','even more stuff')
直接输入到我的脚本中一样
固定文件看起来像这样,仍然使用与上面相同的 ini 示例
import configparser
from configparser import ConfigParser
from ast import literal_eval
config = ConfigParser()
config.read("SortingConfig.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
#The Variable gets Defined
l1catV = literal_eval(config['ConfigFile']['l1cat'])
###Further down in my script as I need to use the variable
xls_book = xlrd.open_workbook(infilepath, formatting_info = True, encoding_override = "cp1252", logfile = open(os.devnull, 'w'))
sheetname = xls_book.sheet_by_index(0).name
df = pd.read_excel(xls_book, engine = 'xlrd',parse_dates = ['Date'])
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%y %H:%M:%S').dt.strftime('%m/%d/%y %H:%M')
## L1 Category keep if isin list
##Variable gets used
df = df.loc[df['L1 Category'].isin(l1catV)]
您可以使用 ast.literal_eval
(doc) 来解析值:
txt = '''
[ConfigFile]
#L1 Category to Keep
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
'''
import configparser
from configparser import ConfigParser
from ast import literal_eval
config = ConfigParser()
#####config.read_string(txt)###### - This portion was incorrect
my_tuple = literal_eval(config['ConfigFile']['l1cat'])
for val in my_tuple:
print(val)
打印:
Audio Terminal
Communications/Telephone
Microphones
Speakers
Tour Sound
我似乎可以正常工作...除了部分解析返回了错误的变量,而且我不确定我遗漏了什么
在我的 config.ini
[section]
#Color 1
color01 = ('00FCC84E')
cat1 = ('Speakers','Atlas Mic Stands')
#Color 2
color02 = ('00F4426E')
cat2 = ('Audio Mixers/Console')
当我使用
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)
一个returns有(),另一个没有.... 我需要做什么才能让两者都返回 ()
('Speakers', 'Atlas Mic Stands')
Audio Mixers/Console