Writing multiple lists to CSV rows in python
<p>I am writing a program that extracts the history from the Google Chrome history database and outputs this to a CSV file. I am trying to put the information in multiple rows, for example a list of URL's in the first row and the webpage Title in the second row. However, when I do this, I receive the following error:</p>

<p>TypeError: decoding Unicode is not supported</p>

<p>Any help would be appreciated, below is my code:</p>

<pre><code>import sqlite3
import datetime
import csv

def urls():
    conn = sqlite3.connect('C:\Users\username\Desktop\History.sql')
    cursor = conn.execute("SELECT url, title, visit_count, last_visit_time from urls")

    timestamp = row[3]
    value = datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=timestamp)

    with open("C:\Users\username\Desktop\\historyulrs.csv", "ab") as filecsv:
        filecsvwriter = csv.writer(filecsv)
        filecsvwriter.writerow(["Url", "Title", "Visit Count", "Last visit Time"])

    for row in cursor:
        with open("C:\Users\username\Desktop\\historyulrs.csv", "ab") as filecsv:
            filecsvwriter = csv.writer(filecsv)
            filecsvwriter.writerows([unicode(row[0], row[1]).encode("utf-8")])

    conn.close()

urls()
</code></pre>

<p>I also retrieve the visit count and last visit time from the database to add to the CSV however. I haven't implemented this yet. </p>

<p>Thanks</p>

<p>This is not easy to answer without seeing the DB. But something like this should work, potentially with a few small modifications depending on your actual data. </p>

<pre><code>import sqlite3
import datetime
import csv

def urls():
    conn = sqlite3.connect('C:\Users\username\Desktop\History.sql')
    c = conn.cursor()
    query = "SELECT url, title FROM urls"
    c.execute(query)
    data = c.fetchall()

    if data:
        with open("C:\Users\username\Desktop\\historyulrs.csv", 'w') as outfile:
            writer = csv.writer(outfile)
            writer.writerow(['URL', 'Title'])
            for entry in data:
                writer.writerow([str(entry[0]), str(entry[1])])
</code></pre>

python sql list csv unicode