Error converting list to string
Question: I am trying to specify the position where I would like my data to be written
in my output file but I am getting the following error. Could I please be
advised on how I might resolve this issue?

    
    
    Traceback (most recent call last):
      File "C:/Python33/Upstream and downstream shores.py", line 20, in <module>
        upstream_shore.writerow (line[0] + [upstream_1] + line[1])
    TypeError: Can't convert 'list' object to str implicitly
    

Here is my code:

    
    
    import csv
    
    file1 = open ('fileA.csv', 'rt', newline ='')
    reader = csv.reader(file1)
    
    upstream = open ('fileB.csv', 'wt', newline = '')
    upstream_shore = csv.writer(upstream)
    
    downstream = open ('fileC.csv', 'wt', newline = '')
    downstream_shore = csv.writer(downstream)
    
    count_upstream = 0
    count_downstream = 0
    
    for line in reader:
        downstream = [(int(x) - 2000) for x in line[2]]
        upstream = [(int(y) - 2000) for y in line [1]]
        upstream_1 = str(upstream)
        downstream_1 = str(downstream)
        upstream_shore.writerow (line[0] + [upstream_1] + line[1])
        count_upstream += 1
        downstream_shore.writerow(line[0] + line [2] + [downstream_1])
        count_downstream += 1
    
     print ('Number of upstream shore counts:', count_upstream)
     print ('Number of downstream shore counts:', count_downstream)
    
    file1.close()
    upstream.close()
    downstream.close()
    

Thanks


Answer: In the following line:

    
    
    upstream_shore.writerow (line[0] + [upstream_1] + line[1])
    

`line[0]` (and `line[1]`) is a string, not a list. Python is trying to
concatenate a string and a list, but it doesn't know how so it gives you the
`TypeError`. You probably want `[line[0]]` instead so that it's concatenating
2 lists (which is knows how to do) e.g.:

    
    
    upstream_shore.writerow ([line[0]] + [upstream_1] + [line[1]])
    

Note, you'll have the same error again 2 lines down.

