How to remove newline characters from csv file
<p>I have the below part of code that reads values from the csv file "prom output.csv" and writes them sorted in a new one "sorted output.csv".</p>

<pre><code>import collections
import csv

with open("prom output.csv","r") as f:
    cr = csv.reader(f,delimiter=",")
    d=collections.defaultdict(lambda : list())
    header=next(cr)   
    for r in cr:
        d[r[0]].append(r[1])  


with open("sorted output.csv","w") as f:
    cr = csv.writer(f,delimiter=",")
    cr.writerow(header)  
    od = collections.OrderedDict(sorted(d.items()))
    for k,v in od.items():  s
        cr.writerow([k,",".join(v)]) 
</code></pre>

<p>However the output (see below) has spaces between each line and i would like to remove them. Can anybody help?</p>

<p><a href="http://i.stack.imgur.com/y6RSD.png" rel="nofollow"><img src="http://i.stack.imgur.com/y6RSD.png" alt="enter image description here"></a></p>

<p>The input file "prom output.csv" has no spaces between each line as seen below</p>

<p><a href="http://i.stack.imgur.com/SpOER.png" rel="nofollow"><img src="http://i.stack.imgur.com/SpOER.png" alt="enter image description here"></a></p>

<p>as a last midification i would like my output to look like: </p>

<p><a href="http://i.stack.imgur.com/o0igJ.png" rel="nofollow"><img src="http://i.stack.imgur.com/o0igJ.png" alt="enter image description here"></a></p>

<p>any suggestions?</p>

<p>The Python newline character is, '\n'. Therefore to remove the newline character (and hence the blank lines that are being printed) from your string, try:</p>

<pre><code>cr.writerow([k, (",".join(v)), .strip("\n")])
</code></pre>

python excel csv