python csv new line
Question: 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

    
    
    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()                                                                                
    

current output

    
    
    "(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')"
    

expected out

    
    
     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
    


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

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

    
    
    for ve in ver:
        ver1.append([str(item).replace(<something>) for item in ve])
    

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

    
    
    with open('filename.csv', 'wb') as csv_file:
         writer = csv.writer(csv_file)
         writer.writerows(ver1)
    

