I'm trying to read some data from a file, and use the values of the file in a specific row as values for my dictionary. One of the values in the file is a variable (filename) that is already configured in my code, but when I'm trying to use the value in the dictionary it's represented as str. is it possible to somehow represent it as a variable name?
Text File:
Query Tests
csv_name_1,Product,MAIL,Protocol,IMAP4,Total in DB
csv_name_1,Product,MAIL,Protocol,IMAP4,Total in DB
csv_name_1,Product,MAIL,Protocol,IMAP4,Total in DB
csv_name_1,Product,MAIL,Protocol,IMAP4,Total in DB
csv_name_1,Product,MAIL,Protocol,IMAP4,Total in DB
Already existed variable
csv_name_1 = "%s/gpdb_%s.csv" % (tmp_dir, today_wo_clock)
csv_name_2 = "%s/gpdb_aa_%s.csv" % (tmp_dir, today_wo_clock)
What I'm trying to run:
lookup = 'csv_name_'
with open('Excel_Mapping.txt') as myFile:
for num, line in enumerate(myFile, 1):
if lookup in line:
data = line.split(sep=',')
if len(data) >= 8:
lov = data
row = {'csv_name': lov[0], 'col1_name': lov[1], 'col1_val': lov[2],
'col2_name': lov[3], 'col2_val': lov[4], 'col3_name': lov[5], 'col3_val': int(lov[6]),
'result_col_name': lov[7], 'sanity_excel_name': sanity_excel_name, 'row': num}
Expected result: csv_name_1 passed as variable and the code using already defined variable with this name.
row = {'csv_name': csv_name_1, 'col1_name': lov[1], 'col1_val': lov[2],
'col2_name': lov[3], 'col2_val': lov[4], 'col3_name': lov[5], 'col3_val': int(lov[6]),
'result_col_name': lov[7], 'sanity_excel_name': sanity_excel_name, 'row': num}
Actual result: csv_name_1 passed as string and of course a failure in execution cause no such file
row = {'csv_name': 'csv_name_1', 'col1_name': lov[1], 'col1_val': lov[2],
'col2_name': lov[3], 'col2_val': lov[4], 'col3_name': lov[5], 'col3_val': int(lov[6]),
'result_col_name': lov[7], 'sanity_excel_name': sanity_excel_name, 'row': num}