I have a script that uses config parser to grab a list using config parser and an ini file using this because the end user will put in his or her own list to use in a sorting script
Example of a part of the config.ini file
[ConfigFile]
#L1 Category to Keep
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
I have this in my script to grab the ini file
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
the problem portion I am having.... is turning the
l1cat=('Audio Terminal', 'Communications/Telephone', 'Microphones', 'Speakers', 'Tour Sound')
into a useable variable value
further into my script I have this
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)]
the Error I get back is TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]
before it needed to be dynamically changed by the end user, I could literally add l1cat = ('stuff','more stuff','even more stuff') as a regular variable and it worked perfectly
and the reason I am using config parser is because when I compile to an EXE, I need something that the variables can be changed anytime by a config file
I actually need it to work Just as if I had entered the l1cat = ('stuff','more stuff','even more stuff')
directly into my script
The Fixed file looks like this still using teh same ini example as above
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)]
You can use ast.literal_eval
(doc) to parse the values:
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)
Prints:
Audio Terminal
Communications/Telephone
Microphones
Speakers
Tour Sound