我有一个CSV文件,我想使用Python将此文件批量导入我的sqlite3数据库.命令是".import .....".但它似乎无法像这样工作.谁能给我一个如何在sqlite3中做到这一点的例子?我正在使用Windows以防万一.谢谢
我在这里遇到了一个问题(我的RAM):它无法保存我想要绘制的数据.我确实有足够的高清空间.是否有任何解决方案可以避免我的数据集"阴影"?
具体而言,我处理数字信号处理,我必须使用高采样率.我的框架(GNU Radio)以二进制形式保存值(以避免使用太多的磁盘空间).我打开包装.之后我需要策划.我需要可缩放的图和交互式.这是一个问题.
是否有任何优化潜力,或其他软件/编程语言(如R左右)可以处理更大的数据集?实际上我想在我的情节中获得更多数据.但我没有其他软件的经验.GNUplot失败,采用与以下类似的方法.我不知道R(喷射).
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import struct
"""
plots a cfile
cfile - IEEE single-precision (4-byte) floats, IQ pairs, binary
txt - index,in-phase,quadrature in plaintext
note: directly plotting with numpy results into shadowed functions
"""
# unpacking the cfile dataset
def unpack_set(input_filename, output_filename):
index = 0 # index of the samples
output_filename = open(output_filename, 'wb')
with open(input_filename, "rb") as f:
byte = f.read(4) # read 1. column of the vector
while byte != …Run Code Online (Sandbox Code Playgroud) 我正在"转换"一个大的(~1.6GB)CSV文件,并将CSV的特定字段插入到SQLite数据库中.基本上我的代码看起来像:
import csv, sqlite3
conn = sqlite3.connect( "path/to/file.db" )
conn.text_factory = str #bugger 8-bit bytestrings
cur = conn.cur()
cur.execute('CREATE TABLE IF NOT EXISTS mytable (field2 VARCHAR, field4 VARCHAR)')
reader = csv.reader(open(filecsv.txt, "rb"))
for field1, field2, field3, field4, field5 in reader:
cur.execute('INSERT OR IGNORE INTO mytable (field2, field4) VALUES (?,?)', (field2, field4))
Run Code Online (Sandbox Code Playgroud)
一切都按照我的预期进行,但例外情况是......它需要花费大量的时间来处理.我编码不正确吗?有没有更好的方法来实现更高的性能并完成我需要的(只需将CSV的几个字段转换为SQLite表)?
**编辑 - 我尝试按照建议直接将csv导入sqlite但事实证明我的文件在字段中有逗号(例如"My title, comma").这导致导入错误.看来手动编辑文件的次数太多了......
还有其他的想法??**
我想使用 Python 向 SQLite 插入 100 万条记录。我尝试了多种方法来改进它,但仍然不太满意。数据库将文件加载到内存使用 0.23 秒(pass在下面搜索)但 SQLite 1.77 秒加载和插入到文件。
英特尔酷睿 i7-7700 @ 3.6GHz
16GB RAM
美光 1100 256GB 固态硬盘,Windows 10 x64
Python 3.6.5 Minconda
sqlite3.version 2.6.0
我使用与我的真实数据相同的格式生成了 100 万个测试输入数据。
import time
start_time = time.time()
with open('input.ssv', 'w') as out:
symbols = ['AUDUSD','EURUSD','GBPUSD','NZDUSD','USDCAD','USDCHF','USDJPY','USDCNY','USDHKD']
lines = []
for i in range(0,1*1000*1000):
q1, r1, q2, r2 = i//100000, i%100000, (i+1)//100000, (i+1)%100000
line = '{} {}.{:05d} {}.{:05d}'.format(symbols[i%len(symbols)], q1, r1, q2, r2)
lines.append(line)
out.write('\n'.join(lines))
print(time.time()-start_time, i)
Run Code Online (Sandbox Code Playgroud)
我在我的Python应用程序中使用Sqlite3数据库并使用参数替换查询它.
例如:
cursor.execute('SELECT * FROM table WHERE id > ?', (10,))
Run Code Online (Sandbox Code Playgroud)
有些查询没有正确返回结果,我想记录它们并尝试手动查询sqlite.
如何使用参数而不是问号来记录这些查询?
python ×5
sqlite ×3
csv ×2
performance ×2
database ×1
logging ×1
matplotlib ×1
optimization ×1
sql-insert ×1
terminology ×1