I am using standard library string template
. From the template, I make a list that contains all the identifiers. For example:
list_of_identifiers = ['id', 'username', 'url']
I want to iterate this list to substitute the template identifier.
xml = string.Template(xml_template)
for i in range(len(list_of_identifiers)):
xml.substitute(list_of_identifiers[i] = somevalue)
But I am getting a syntax error SyntaxError: keyword can't be an expression
.
I want to use the string literal in the list_of_identifiers[i]
as a keyword. Is it possible?
Edit
What I am basically doing here is reading a csv file with the values of identifiers and then substituting the values into the xml template. But the csv file can include other fields than just the identifiers. In other words the csv file could read:
id, username, orientation, eye, expression, url
1, admin, left, sunglasses, sad, http://google.com
However the identifiers that I want to substitute are [id, username, url]
only. So I generate two lists:
list_of_identifiers = ['id', 'username', 'url']
col_index = ['0','1','5']
As you might tell, col_index is the the indexes of the identifiers in the csv row. So I iterate both of the lists:
with open(dataFile, 'rb') as csvFile_obj:
reader = csv.reader(csvFile_obj)
for row in reader:
#
#some code to filter out first line of csv file
#
xml = string.Template(xml_template)
for i in range(len(list_of_identifiers)):
xml.substitute(list_of_identifiers[i]= row[col_index[i]])