Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert output from sqlite from tuple to list in Python

Tags:

python

sqlite

csv

I am new to programming and am writing a simple python script that will gather some information from a PC for forensic purposes. Some of the information I want is the Chrome history. Basically I need to read the data from the history sqlite database, perform a calculation to convert the time stamp to a human readable format, and then write the data to a CSV file. I have the following code:

connection = sqlite3.connect('c:\history')### need correct path
connection.text_factory = str
cur = connection.cursor()
output_file = open('chrome_history2.csv', 'wb')
csv_writer = csv.writer(output_file,)
headers = ('URL', 'Title', 'Visit Count', 'Last Visit')
csv_writer.writerow(headers)
epoch = datetime(1601, 1, 1)
for row in (cur.execute('select url, title, visit_count, last_visit_time from urls limit 10')): #selects data
        list(row) #convert to list - does not work
        url_time = epoch + timedelta(microseconds=row[3]) #calculates time
        row[3] = url_time #changes value in row to readable time
        csv_writer.writerow(row)
connection.close()

This code returns the error:

TypeError: 'tuple' object does not support item assignment

I understand that I can't manipulate the data in a tuple but I am explicitly converting each row to a list. Can anyone explain a) why list (row) does not work, and b) a better way to do it?

Thanks in advance!

like image 897
David White Avatar asked May 12 '26 06:05

David White


1 Answers

I think that:

        list(row) #convert to list - does not work

Should be:

        row = list(row) #convert to list

In addition, you may want to look into using a csv.DictReader object instead of the default reader, as it may be more convenient (and is mutable, unlike the default tuple). See https://docs.python.org/2/library/csv.html#csv.DictReader for detauls.

like image 161
shevron Avatar answered May 14 '26 22:05

shevron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!