我想将列表值存储在变量中,然后必须将其写入文件

时间:2012-07-01 07:32:30

标签: python file list variables

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]

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

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

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

write_file.write_file(Name , Filename)

1 个答案:

答案 0 :(得分:0)

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

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循环为数据库中的每个值执行此操作。