3

I have a list of commands that I want to iterate over so I've put those commands into a list. However, I want to also use that list as strings to name some files. How do I convert variable names to strings?

itemIDScore = """SELECT * from anytime;"""

queryList = [itemIDScore, accountScore, itemWithIssue, itemsPerService]
    for x in queryList:

    fileName = x+".txt"
    cur.execute(x) #This should execute the SQL command
    print fileName #This should return "itemIDScore.txt"

I want fileName to be "itemIDScore.txt" but itemIDScore in queryList is a SQL query that I'll use elsewhere. I need to name files after the name of the query.

Thanks!

Publiccert
  • 193
  • 4
  • 17
  • 3
    So ... you want something such as "itemIDScore.txt" as a file name? Or do you want the string value of the variable itemIDScore? – Prune Apr 21 '17 at 17:57
  • I want something like "itemIDScore.txt" for use in other functions but I'll call the string value of itemIDScore in other functions. – Publiccert Apr 21 '17 at 17:59
  • Please **clarify your specific problem or add additional details to highlight exactly what you need**. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question – Pedro Lobito Apr 21 '17 at 18:03

4 Answers4

3

I don't think you may get name of the variable as string from the variable object. But instead, you may create the list of string of your variables as:

queryList = ['itemIDScore', 'accountScore', 'itemWithIssue', 'itemsPerService']

Then you may access the value of variable from the variable name string using the globals() function as:

for x in queryList:
    fileName = "{}.txt".format(x)
    data = globals()[x]
    cur.execute(data) 

As the globals() document say:

Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
3

As far as I know, there is no easy way to do that, but you could simply use a dict with what currently are variable names as keys, e.g.:

queries = {
    'itemIDScore': 'sql 1', 
    'accountScore': 'sql 2',
    ...
}

for x in queries:
    fileName = x + ".txt"
    cur.execute(queries[x])
    print fileName

This would also preserve your desired semantics without making the code less readable.

Vadim Landa
  • 2,784
  • 5
  • 23
  • 33
  • If the value assigned to the variables are the static values like string and the variables are related with each other as mentioned here in the example, usage of dictionary is the correct way. – Moinuddin Quadri Apr 21 '17 at 18:16
1

I think you would have an easier time storing the names explicitly, then evaluating them to get their values. For example, consider something like:

itemIDScore = "some-long-query-here"
# etc.
queryDict = dict( (name,eval(name)) for name in ['itemIDScore', 'accountScore', 'itemWithIssue', 'itemsPerService'] )
for k in queryDict:
  fileName = k+".txt"
  cur.execute(queryDict[k])
MassPikeMike
  • 672
  • 3
  • 12
0

You can use the in-built str() function.

for x in queryList:
   fileName = str(x) + ".txt"
   cur.execute(x)