Skip to main content
 首页 » 编程设计

python之将变量列表转换为字符串

2025年05月04日22kerrycode

<分区>

我有一个要迭代的命令列表,所以我将这些命令放入列表中。但是,我还想将该列表用作字符串来命名一些文件。如何将变量名转换为字符串?

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" 

我希望 fileName 为“itemIDScore.txt”,但 queryList 中的 itemIDScore 是我将在其他地方使用的 SQL 查询。我需要在查询名称后命名文件。

谢谢!