使用python将变量连接成一个大变量

时间:2016-10-06 23:11:42

标签: python string loops variables concatenation

我在python中有以下程序,它应该从数组中打印值。

import sys
from datetime import *
import pprint
txtfile=sys.argv[1]
f = open(txtfile,'r')
lines=f.readlines()
f.close

for line in lines:
    col = line.split(',')
    wrow=[]
    grid=[]

    lon=int(round(float(col[0]),0))
    lat=int(round(float(col[1]),0))
    val=float(col[2])
    my_tuple=(lon,lat,val)
    wrow.append(my_tuple)

    if(len(wrow)==720):
        row = wrow[0:] + wrow[:720]
        grid.append(row)
        wrow=[]
        row=[]

    if(len(grid)==360):
          grid.reverse()
         for row in grid:
            string=''
            i=1
            for mytuple in row:
                 print "count is ",i
                 val=str(mytuple[2])
                 print "val is ",val
                 #string = string + val + '\t'
                 #print "string is ",string
                 i=i+1
                 if(i==4):
                     print "****************************"
                     string = string + val + '\t'
                     #print string
                     #string = string + '\n'
                     i=1
                 else:
                     string = string + '\n'
                     #print string
          #pprint.pprint(grid)
          grid=[]
          wrow=[]



exit()

该程序的输出如下所示:

****************************
count is  1
val is  0.51
count is  2
val is  0.69
count is  3
val is  0.83
****************************
count is  1
val is  0.7
count is  2
val is 0.59
count is 3 
val is 0.93
*****************************

现在我想以这样一种方式设置python脚本,即我有一个连接所有变量val的变量字符串。因此,我有:

print "***************************"
print string

我会得到输出:

*******************************
0.51 0.69 0.83
*******************************
0.7 0.59 0.93

有关如何调整此脚本的任何想法吗?

1 个答案:

答案 0 :(得分:2)

你必须做某种循环或连接才能将它们全部放在同一条线上,但它并没有那么糟糕。我会做这样的事情:

application/x-www-form-urlencoded