Python 如何在循环中使用fetchone()将值添加到值列表中?
我认为这是错误的,因为我将fetchone()更改为具有dict_factory函数的字典,而不是元组,但我希望它是一个字典,我还希望将值作为列表添加到键中Python 如何在循环中使用fetchone()将值添加到值列表中?,python,mongodb,dictionary,sqlite,Python,Mongodb,Dictionary,Sqlite,我认为这是错误的,因为我将fetchone()更改为具有dict_factory函数的字典,而不是元组,但我希望它是一个字典,我还希望将值作为列表添加到键中 def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d def open_sql(sql_folder, sql_name,
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def open_sql(sql_folder, sql_name, sql_table):
# databases are located at /work/jmjohnso1/db_project
path_name = os.path.join(sql_folder,sql_name).strip()
con = lite.connect(path_name)
con.row_factory = dict_factory
cur = con.cursor()
cur.execute('SELECT * FROM ' + sql_table)
dict_contents = defaultdict(list)
while cur.fetchone() != None:
cur.fetchone()
for k, v in cur.fetchone():
dict_contents[k].append(v)
con.close()
pprint(dict_contents)
return dict_contents
期望输出:
{'atime': [1141682141, 1141682142],
'attr_id': [3, 2]
}
我得到的错误是:
for k, v in cur.fetchone():
ValueError: too many values to unpack (expected 2)
全部代码
# python3.5
# pymongo version 3.2.2
# MongoDB shell version: 3.0.11
import os
import pymongo
from pymongo import MongoClient
import sqlite3 as lite
import pyewf
import hashlib
from itertools import chain
from collections import defaultdict
import pprint
def list_sql_db(folder):
# need a list for multiprocessing so I made a file.
file_name = os.path.join(folder, 'sql_db')
if not os.path.isfile(file_name):
with open (file_name, 'w') as line:
for (dirpath, dirs, files) in os.walk(folder):
for name in files:
line.write(name + '\n')
return file_name
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def open_sql(sql_folder, sql_name, sql_table):
# databases are located at /work/jmjohnso1/db_project
path_name = os.path.join(sql_folder,sql_name).strip()
con = lite.connect(path_name)
con.row_factory = dict_factory
cur = con.cursor()
cur.execute('SELECT * FROM ' + sql_table)
dict_contents = defaultdict(list)
while cur.fetchone() != None:
cur.fetchone()
for k, v in cur.fetchone():
dict_contents[k].append(v)
con.close()
pprint(dict_contents)
return dict_contents
def insert_tsk_mongo(sql_folder, sql_name, sql_table):
client = MongoClient() # connect to mongodb
db = client.nus # make or use a db called nus
contents = open_sql(sql_folder, sql_name, sql_table)
collection = sql_name.strip().replace('-','_') # because mongo will write but not read a collection with -
# document_id = db[collection].insert({ # sql_name is the hard drive name
# sql_table:
# contents
# })
###############################################################################
sql_folder = '/work/jmjohnso1/db_project'
# sql_tables = ['tsk_fs_info', 'tsk_image_info',
# 'tsk_db_info ', 'tsk_image_names',
# 'tsk_file_layout', 'tsk_objects',
# 'tsk_files', 'tsk_vs_info', 'tsk_vs_parts']
sql_tables = ['tsk_files']
sql_folder_name = list_sql_db(sql_folder)
with open (sql_folder_name, 'r') as read:
sql_names = read.readlines()
for sql_name in sql_names:
for sql_table in sql_tables:
insert_tsk_mongo(sql_folder, sql_name, sql_table)
break
请尝试将代码中的光标提取指令替换为以下指令:
for k, v in cur.fetchone().items():
只要您的dict_工厂返回一个dict,当获取对它的引用时,您就必须迭代它包含的项,而不是它的引用。当它是一个元组时,cur.fetchone()简单而正确地完成了这项工作。请尝试将代码中的游标抓取指令替换为以下指令:
for k, v in cur.fetchone().items():
只要您的dict_工厂返回一个dict,当获取对它的引用时,您就必须迭代它包含的项,而不是它的引用。当它是一个元组时,cur.fetchone()简单而正确地完成了这项工作。你认为这是错误的吗?当你运行它时会发生什么?您应该显示代码的功能以及所需的输出。由于您没有提供主程序,我们无法为您检查。错误的单词我知道它是错误的,因为我得到了一个错误。我想我的答案是为什么它不起作用。我没有把所有的东西都放进去,因为我放了太多的抱怨,意见。。。正确的?对于cur.fetchone()中的k,v:ValueError:太多的值无法解包(预期为2)啊,一直流行的
AnError
。你认为这是错误的吗?当你运行它时会发生什么?您应该显示代码的功能以及所需的输出。由于您没有提供主程序,我们无法为您检查。错误的单词我知道它是错误的,因为我得到了一个错误。我想我的答案是为什么它不起作用。我没有把所有的东西都放进去,因为我放了太多的抱怨,意见。。。正确的?对于cur.fetchone()中的k,v:ValueError:太多的值无法解包(预期为2)啊,一直流行的AnError
。