[英]Writing multiple lists to CSV rows in python
I am writing a program that extracts the history from the Google Chrome history database and outputs this to a CSV file. 我正在编写一个程序,该程序从Google Chrome历史记录数据库中提取历史记录,并将其输出到CSV文件。 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.
我正在尝试将信息放在多行中,例如,第一行中的URL列表,第二行中的网页标题。 However, when I do this, I receive the following error:
但是,当我这样做时,会出现以下错误:
TypeError: decoding Unicode is not supported TypeError:不支持解码Unicode
Any help would be appreciated, below is my 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()
I also retrieve the visit count and last visit time from the database to add to the CSV however. 我还从数据库中检索访问次数和上次访问时间,然后添加到CSV中。 I haven't implemented this yet.
我还没有实现。
Thanks 谢谢
Using Pandas can help you a lot with CSV files: 使用熊猫可以为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()
Be aware that I completely guessed the order of data in a row and you would need to edit that according to your needs. 请注意,我完全猜到了连续的数据顺序,您需要根据需要进行编辑。 Also I didn't quite catch why do you need
datetime
. 我也不太明白你为什么需要
datetime
。
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.
但是类似的事情应该起作用,根据您的实际数据,可能需要进行一些小的修改。
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])])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.