python 读取内存_大多数内存有效的方法在Python中读取缓冲区中的块

参见英文答案 >

How do I decrease the memory used by a large list in python 2个

我有一个行文本文件(几GB和~12百万行),其中每行是一个点x,y,z,附件信息.我希望读取文件块,处理点和分割(基于点的位置的空间索引遵循0.25米的方格),将结果存储在临时文件夹中的多个文本文件中.

449319.34;6242700.23;0.38;1;1;1;0;0;42;25;3;17;482375.326087;20224;23808;23808

449310.72;6242700.22;0.35;3;1;1;0;0;42;23;3;17;482375.334291;20480;24576;24576

449313.81;6242700.66;0.39;1;1;1;0;0;42;24;3;17;482375.342666;20224;24576;24576

449298.37;6242700.27;0.39;1;1;1;0;0;42;21;3;17;482375.350762;18176;22784;23552

449287.47;6242700.06;0.39;11;1;1;0;0;42;20;3;17;482375.358921;20736;24832;24832

449290.11;6242700.21;0.35;1;1;1;0;0;42;20;3;17;482375.358962;19968;24064;23808

449280.48;6242700.08;0.33;1;1;1;0;0;42;18;3;17;482375.367142;22528;25856;26624

449286.97;6242700.44;0.36;3;1;1;0;0;42;19;3;17;482375.367246;19712;23552;23296

449293.03;6242700.78;0.37;1;1;1;0;0;42;21;3;17;482375.367342;19456;23296;23808

449313.36;6242701.92;0.38;6;1;1;0;0;42;24;3;17;482375.367654;19968;24576;24576

449277.48;6242700.17;0.34;8;1;1;0;0;42;18;3;17;482375.375420;20224;23808;25088

449289.46;6242700.85;0.31;3;1;1;0;0;42;20;3;17;482375.375611;18944;23040;23040

在哪里“;”是分隔符,前两列x和y对于给出ID位置是有用的

输出结果是另一个文本文件,其中每个ID只随机提取一个点

例如:

20;10;449319.34;6242700.23;0.38;1;1;1;0;0;42;25;3;17;482375.326087;20224;23808;23808

20;10;449310.72;6242700.22;0.35;3;1;1;0;0;42;23;3;17;482375.334291;20480;24576;24576

20;10;449313.81;6242700.66;0.39;1;1;1;0;0;42;24;3;17;482375.342666;20224;24576;24576

20;10;449298.37;6242700.27;0.39;1;1;1;0;0;42;21;3;17;482375.350762;18176;22784;23552

20;11;449287.47;6242700.06;0.39;11;1;1;0;0;42;20;3;17;482375.358921;20736;24832;24832

20;11;449290.11;6242700.21;0.35;1;1;1;0;0;42;20;3;17;482375.358962;19968;24064;23808

前两列是ID

最终输出将是(示例)没有ID值

20;10;449313.81;6242700.66;0.39;1;1;1;0;0;42;24;3;17;482375.342666;20224;24576;24576

20;11;449287.47;6242700.06;0.39;11;1;1;0;0;42;20;3;17;482375.358921;20736;24832;24832

我正在使用blog的解决方案

# File: readline-example-3.py

file = open("sample.txt")

while 1:

lines = file.readlines(100000)

if not lines:

break

for line in lines:

pass # do something

我的代码如下:

from __future__ import division

import os

import glob

import tempfile

import sys

def print_flulsh(n, maxvalue = None):

sys.stdout.write("\r")

if maxvalue is None:

sys.stdout.write("Laser points processed: %d" % n)

else:

sys.stdout.write("%d of %d laser points processed" % (n, maxvalue))

sys.stdout.flush()

def point_grid_id(x, y, minx, maxy, size):

"""give id (row,col)"""

col = int((x - minx) / size)

row = int((maxy - y) / size)

return row, col

def tempfile_tile_name(line, temp_dir, minx, maxy, size, parse):

x, y = line.split(parse)[:2]

row, col = point_grid_id(float(x), float(y), minx, maxy, size)

return os.path.normpath(os.path.join(temp_dir + os.sep,"tempfile_%s_%s.tmp" % (row, col)))

# split the text file in small text files following the ID value given by tempfile_tile_name

# where:

# filename : name+path of text file

# temp_dir: temporary folder

# minx, maxy: origin of the grid (left-up corner)

# size: size of the grid

# parse: delimeter of the text file

# num: number of lines (~ 12 millions)

def tempfile_split(filename, temp_dir, minx, maxy, size, parse, num):

index = 1

with open(filename) as file:

while True:

lines = file.readlines(100000)

if not lines:

break

for line in lines:

print_flulsh(index, num)

index += 1

name = tempfile_tile_name(line, temp_dir, minx, maxy, size, parse)

with open(name, 'a') as outfile:

outfile.write(line)

我的代码的主要问题是当临时文件夹中保存了大约200万个分割文本文件时速度会降低.如果有一个优化的方法来创建缓冲区,我想知道effbot.org的解决方案吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值