在python中将多个列表写入CSV行

时间:2016-09-20 16:14:56

标签: python sql list csv unicode

我正在撰写一个程序,从Google Chrome历史数据库中提取历史记录并将其输出到CSV文件。我试图将信息放在多行中,例如第一行中的URL列表和第二行中的网页标题。但是,当我这样做时,我收到以下错误:

TypeError:不支持解码Unicode

任何帮助将不胜感激,下面是我的代码:

function myPages(origin,div_tag) //origin is an identifier of where the pagination links live
//div_tag indicates where the resultant page will be output to.
{
  mydata={tag: div_tag};
  $(origin).off('click','.pagination a',mydata, directPages);
  $(origin).on('click','.pagination a',mydata, directPages);
}
//event handles are passed the event always, any additional data included is passed
// as a property (i.e. event.data).
function directPages(e)
{
  e.preventDefault();
  e.stopPropagation();
  var url=$(this).attr('href');
  var tag = e.data.tag;
  loadComponent(url,tag);
}

我还从数据库中检索访问次数和上次访问时间,然后添加到CSV。我还没有实现这个。

由于

2 个答案:

答案 0 :(得分:1)

使用Pandas可以为CSV文件提供很多帮助:

import sqlite3
import datetime
import pandas

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

    for row in cursor:
    #now I am just guessing
        urls.append(row[0])
        titles.append(row[1])
        counts.append(row[2])
        last.append(row[3])

    df = pandas.DataFrame({'URL': urls,
                           'Title': titles,
                           'Visit Count': counts,
                           'Last visit Time': last})

    df.to_csv('historyulrs.csv', encoding='utf-8', index=False)
    conn.close()

urls()

请注意,我已经完全猜到了连续数据的顺序,您需要根据需要进行编辑。另外,我不太清楚为什么你需要datetime

答案 1 :(得分:1)

如果不看DB,这很难回答。但是这样的事情应该可行,可能会根据您的实际数据进行一些小的修改。

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