This topic created in 1360 days ago, the information mentioned may be changed or developed.
我现在的读取存方法太慢了,希望大佬能给点建议提升效率.
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 replies 2022-08-05 10:39:04 +08:00  | | 3 CaptainD Aug 5, 2022 用 pandas 批量处理 txt ,然后批量构建 obj ,再插入,然后再分块多进程,这个思路是不是好点 |
 | | 4 vhysug01 Aug 5, 2022 这类型数据我存过,用的是 bcolz 存的,文件形式落盘 |
 | | 5 httplife Aug 5, 2022 空白键换成 , 另存为 csv. 然后 import? |
 | | 7 SenLief Aug 5, 2022 把 txt 先整成 csv ,那就效率高了。 |