【问题标题】:I want to store list value in a variable and then have to write it into a file我想将列表值存储在变量中,然后必须将其写入文件
【发布时间】:2012-07-02 01:51:12
【问题描述】:
value,rows = DB.DB_sel('localhost','root','ndoitadmin','Example','select * from UC1_voip_gateway')

我在这里以列表格式从数据库中获取值

for j in range(0,rows):
     for i in range(0,21):
        # print value[j][i]

这里我必须以句子格式打印列表的值,以便我可以将变量名称传递给 write.file 函数以将其写入文件

           Name = ''' '+value[0][1]+' 
                   list 2 = '+value[0][2]+'

                   .....etc ....'''

write_file.write_file(Name , Filename)

【问题讨论】:

  • 您的意思是要将数据序列化并输出到文件中吗?

标签: python file list variables


【解决方案1】:

更好地描述你想做的事情会有所帮助,但这是我能理解的答案:

outfile = open(file.txt, 'w') # Opens the output file

for j in range(0, rows):
    for i in range(0, 21) # I suppose this is the way how you obtain your "value"
        outfile.write("Value1 is: %s\nValue2 is: %s" % (value[0], value[1])

outfile.write("Value1 is: %s\nValue2 is: %s" % (value[0], value[1]) 行的输出是:

Value1 is: x
Value2 is: y

for 循环对数据库中的每个值都执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2021-10-12
    • 1970-01-01
    相关资源
    最近更新 更多