这是一个创建于 792 天前的主题,其中的信息可能已经有所发展或是发生改变。
我现在的读取存方法太慢了,希望大佬能给点建议提升效率.
def file_name(url):
for root, dirs, files in os.walk(url):
for i in range(len(files)):
f = open('c:/test/txt/file/' + files[i][:files[i].index('.txt')] + '.txt')
next(f)
for j in f:
res = j.split(' ')
obj = {}
for k in res:
if len(res) > 2:
obj = {
"trade_date": res[0],
"time": res[1],
"open": float(res[2]) if res[2] else '',
"high": float(res[3]) if res[3] else '',
"low": float(res[4]) if res[4] else '',
"close": float(res[5]) if res[5] else '',
"vol": float(10)
}
if obj and obj['trade_date'] != '':
name = files[i][:files[i].index('.txt')]
origin = client['abc'][name]
origin.insert_one(obj)
# print()
print(i, files[i][:files[i].index('.txt')], 'end')
file_name('c:/test/txt/file/')
txt 文件:
B2210 豆二 2210 1 分钟线 前复权
日期 时间 开盘 最高 最低 收盘 成交量 持仓量 结算价
20211025 0901 3853 3853 3853 3853 0 0 0
7 条回复 • 2022-08-05 10:39:04 +08:00
|
|
1
kaedeair 2022-08-05 09:19:09 +08:00
任务切块扔进程池
|
|
|
2
tairan2006 2022-08-05 09:27:03 +08:00 1
你不要一条一条插啊…批量写入
|
|
|
3
CaptainD 2022-08-05 09:53:16 +08:00
用 pandas 批量处理 txt ,然后批量构建 obj ,再插入,然后再分块多进程,这个思路是不是好点
|
|
|
4
vhysug01 2022-08-05 09:57:15 +08:00
这类型数据我存过,用的是 bcolz 存的,文件形式落盘
|
|
|
5
httplife 2022-08-05 10:00:47 +08:00
空白键换成 , 另存为 csv. 然后 import?
|
|
|
6
root000 2022-08-05 10:37:48 +08:00
|
|
|
7
SenLief 2022-08-05 10:39:04 +08:00
把 txt 先整成 csv ,那就效率高了。
|