concatenating variables into one large variable with python
<p>I have the below program in python which is supposed to print values from an array.</p>

<pre><code>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()
</code></pre>

<p>The output of this program looks something like this:</p>

<pre><code>****************************
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
*****************************
</code></pre>

<p>Now I want to set up the python script in such a way that I have a variable string that concatenates all the variables val.  Therefore is I have:</p>

<pre><code>print "***************************"
print string
</code></pre>

<p>I would get the output:</p>

<pre><code>*******************************
0.51 0.69 0.83
*******************************
0.7 0.59 0.93
</code></pre>

<p>Any ideas on how to tweak this script to do this?</p>

<p>You'll have to do some kind of loop or join to put them all on the same line, but it's not that bad. I'd do something like this:</p>

<pre><code>tuples = [(1, .51), (2, 0.69), (3, 0.83)]
joined = " ".join([repr(x[1]) for x in tuples])
print (joined)

'0.51 0.69 0.83'
</code></pre>

python string loops variables concatenation