Converting list of variables to strings

Publiccert

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!

Anonymous

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting list of strings to datetime

Converting a list of strings to dictionary

Converting a list of timestamp strings

Converting unknown amount of strings into variables

Converting list of strings to list of tuples

Converting a list of strings into a list of floats

Python converting a list of strings to unicode

Converting string of strings to list in dataframe

Python converting strings in a list to numbers

Converting list of strings with decimals into datetime

Converting List of Values into Encoded Variables?

converting variables in a shell script from strings to numbers

Python converting list of strings to list of integers

Converting a list of strings to list of int in pandas

Converting certain strings with a list into a nested list

Converting list of strings to list of ints in Common Lisp

converting list of integers ,numerical strings into a integer list

Converting a list of strings into a nested list of floats and strings skipping the first item

Having trouble converting a list of strings into numbers

Converting a list of strings into part of a f string in Python

C - converting string (sentence) into strings list

Converting a list to double quoted comma separated strings

Converting a list of strings into floats/ints in F#

Converting a JSON strings list to a JSON array in Python

R - Converting list of separating strings into a string

Converting the first element in a list of strings to an integer

Converting a list of integers to strings using map()

Python 2.7 converting list of strings to dictionary

Converting list of lists of strings into integer in Python