python csv new line
<p>iam using this code to convert db table to csv file, it is  converting in to csv but instead of new line / line breck its using double quotes , can someone help me                                                                                                                                               </p>

<pre><code>import MySQLdb
import csv
conn = MySQLdb.connect(user='root',  db='users', host='localhost')
cursor = conn.cursor()
cursor.execute('SELECT * FROM users.newusers')
ver = cursor.fetchall()
ver1 = []
for ve in ver:
    ve = str(ve).replace("', '","|").replace(", '","|").replace(","," ").replace("  "," ").replace("|",",")
    ver1.append(ve)
print ver1
csv_file = open('filename', 'wb')
writer = csv.writer(csv_file)
writer.writerow(ver1)       csv_file.close()                                                                                
</code></pre>

<p>current output </p>

<pre><code>"(114L,New,9180971675,Ravi Raju,RAJRAVI,National,#N.A,No,No,No,#N.A,OS40,005056BB0803,192.168.0.1,no,yes')","(115L,New,9180971676,Rajendran Mohan,rajemoh,National,#N.A,No,No,No,#N.A,OS40,005056BB0803,192.168.10.10,no,yes')"
</code></pre>

<p>expected out </p>

<pre><code> 114L,New,9180971675,Ravi Raju,RAJRAVI,National,#N.A,No,No,No,#N.A,OS40,005056BB0803,192.168.0.1,no,yes


 115L,New,9180971676,Rajendran Mohan,rajemoh,National,#N.A,No,No,No,#N.A,OS40,005056BB0803,192.168.10.10,no,yes
</code></pre>

<p>SQL queries will return results to you in a list of tuples from <code>fetchall()</code>. In your current approach, you iterate through this list but call <code>str()</code> on each tuple, thereby converting the whole tuple to its string representation.</p>

<p>Instead, you could use a list comprehension on each tuple both to get it into a nested list format and apply your <code>replace()</code> operations.</p>

<pre><code>for ve in ver:
    ver1.append([str(item).replace(&lt;something&gt;) for item in ve])
</code></pre>

<p>In the final step you use <code>csv.writerow()</code> which is designed to be used within a <code>for</code> loop. If your list is nested in the form <code>[[row1], [row2]]</code> then you can just change this to writerow<strong>s</strong>. Also, it's best practice to use the context manager <code>with</code> to handle files as this will automatically close the file once the operation is completed.</p>

<pre><code>with open('filename.csv', 'wb') as csv_file:
     writer = csv.writer(csv_file)
     writer.writerows(ver1)
</code></pre>

python mysql-python