- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有许多非常大的文本文件需要处理,最大的大约 60GB。
每行在七个字段中有 54 个字符,我想从前三个字段中的每个字段中删除最后三个字符 - 这应该会减少大约 20% 的文件大小。
我是 Python 的新手,我有一个代码可以以每小时 3.4 GB 的速度完成我想做的事情,但要成为一项有值(value)的练习,我确实需要至少 10 GB/小时 - 有没有加快速度的方法?这段代码并没有接近挑战我的处理器,所以我猜测它受到内部硬盘驱动器读写速度的限制?
def ProcessLargeTextFile():
r = open("filepath", "r")
w = open("filepath", "w")
l = r.readline()
while l:
x = l.split(' ')[0]
y = l.split(' ')[1]
z = l.split(' ')[2]
w.write(l.replace(x,x[:-3]).replace(y,y[:-3]).replace(z,z[:-3]))
l = r.readline()
r.close()
w.close()
任何帮助将不胜感激。我在 Windows 7 上使用 IDLE Python GUI 并拥有 16GB 内存 - 也许不同的操作系统会更高效?
编辑:这是要处理的文件的摘录。
70700.642014 31207.277115 -0.054123 -1585 255 255 255
70512.301468 31227.990799 -0.255600 -1655 155 158 158
70515.727097 31223.828659 -0.066727 -1734 191 187 180
70566.756699 31217.065598 -0.205673 -1727 254 255 255
70566.695938 31218.030807 -0.047928 -1689 249 251 249
70536.117874 31227.837662 -0.033096 -1548 251 252 252
70536.773270 31212.970322 -0.115891 -1434 155 158 163
70533.530777 31215.270828 -0.154770 -1550 148 152 156
70533.555923 31215.341599 -0.138809 -1480 150 154 158
最佳答案
这样写你的代码更惯用
def ProcessLargeTextFile():
with open("filepath", "r") as r, open("outfilepath", "w") as w:
for line in r:
x, y, z = line.split(' ')[:3]
w.write(line.replace(x,x[:-3]).replace(y,y[:-3]).replace(z,z[:-3]))
这里的主要节省是只执行一次 split
,但如果 CPU 没有被征税,这可能几乎没有什么区别
它可能有助于一次保存几千行并将它们写入一次以减少硬盘驱动器的抖动。一百万行只有 54MB 的 RAM!
def ProcessLargeTextFile():
bunchsize = 1000000 # Experiment with different sizes
bunch = []
with open("filepath", "r") as r, open("outfilepath", "w") as w:
for line in r:
x, y, z = line.split(' ')[:3]
bunch.append(line.replace(x,x[:-3]).replace(y,y[:-3]).replace(z,z[:-3]))
if len(bunch) == bunchsize:
w.writelines(bunch)
bunch = []
w.writelines(bunch)
@Janne 建议,另一种生成线条的方法
def ProcessLargeTextFile():
bunchsize = 1000000 # Experiment with different sizes
bunch = []
with open("filepath", "r") as r, open("outfilepath", "w") as w:
for line in r:
x, y, z, rest = line.split(' ', 3)
bunch.append(' '.join((x[:-3], y[:-3], z[:-3], rest)))
if len(bunch) == bunchsize:
w.writelines(bunch)
bunch = []
w.writelines(bunch)
关于python - 逐行处理非常大 (>20GB) 的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16669428/
我正在使用 python 加密一些文件,但我在逐 block 读取文件时遇到问题。 有时不会返回最后一个 block 的所有数据。 当文件长度为 307200 字节时,我没有问题。当它的长度为 279
我正在使用 WebRTC 将文件发送到连接的对等方,并且我正在以块的形式发送文件。但是,我无法弄清楚如何让对等方在文件逐块流入时保存/下载文件。 我在网上找到的所有例子都推荐做这样的事情: // se
我用 Tiled 做了一张 map 。它的每一 block 图 block 都尺寸为 32x32 像素,我的主要角色 Sprite 也是。 在我的类(class) Player.cpp 中,我有一些计
我见过一些单页网站,您可以逐 block 滚动,因此您没有无限滚动。 你逐 block 移动。 是否有提供此功能的任何脚本或其他东西? 最佳答案 我自己从未使用过它,所以我无法在代码方面为您提供帮助,
这是一个逐 block 反转文件内容的程序。 #include #include #define BS 12 void reverse(char * buffer, int size) { c
在下面的代码中,有没有办法避免 if 语句? s = 13; /*Total size*/ b = 5; /*Block size*/ x = 0; b1 = b; while(x s)
我正在尝试分割输入图像并逐个对其进行模糊处理,但毕竟对相邻图 block 调用 cv::blur 我得到了边界像素,这与我有一次将 cv::blur 集体应用于整个图像。 Mat upper(im,
我想逐个读取文件。该文件被分成几部分,存储在不同类型的媒体上。我目前所做的是调用文件的每个单独部分,然后将其合并回原始文件。 问题是我需要等到所有 block 都到达后才能播放/打开文件。是否可以在
我有一个包含客户和日期列表的 JSON 文件。 文件看起来像这样: { "Customers": [ { "Customer": "Customer Name Here", "Company"
我的邮件目标是从连接到HTTP服务器的TCP套接字读取数据,然后解析 HTTP响应块(传输编码:分块)-服务器在同一连接上每30秒发送一个块 我附上了我的代码。看起来io.Copy读取第一个块,然后等
我认为自己是一位经验丰富的 numpy 用户,但我无法找到以下问题的解决方案。假设有以下数组: # sorted array of times t = numpy.cumsum(numpy.rando
当我将文件添加到暂存区时,我可以 $ git add my_file -p 然后选择我要暂存的 block 。 有没有办法 merge/挑选一个提交并逐 block 应用它的差异? 谢谢 最佳答案 我
我有一个 mongodb 查询,它获取大约 50,000 个大文档。 这对我的 RAM 来说太多了,因此计算机速度变慢了。 现在我想逐 block 迭代 mongodb 结果。 我想获取前 1000
我不会为 AES 或其他加密打开此线程,因为这是我要用来加密 AES 和其他加密的 key 的内容。我从 StackOverflow 和其他一些网站收集了一些代码,并对其进行了编辑以适合我的程序,但是
我在做一些后台工作时尝试收集所有系统统计数据。例如,我使用以下命令来收集 IO 统计信息: iostat -xty 5 此脚本用于每 5 秒收集一次 I/O 统计信息。所以我的日志会包含很多数据 bl
我需要 php 脚本,用于从 url 到服务器的可恢复文件下载。它应该能够开始下载,然后在捕捉时(30 秒 - 5 分钟)恢复,依此类推,直到完成整个文件。 perl 中有类似的东西 http://c
是否有标准的 Linux 命令可用于逐 block 读取文件?例如,我有一个大小为 6kB 的文件。我想读取/打印第一个 1kB,然后是第二个 1kB ...似乎 cat/head/tail 在这种情
我正在处理大量文件,我想逐 block 处理这些文件,假设在每批处理中,我想分别处理每 50 个文件。 如何使用 Spark Structured Streaming 来实现? 我看到 Jacek L
我正在处理大量文件,我想逐 block 处理这些文件,假设在每批处理中,我想分别处理每 50 个文件。 如何使用 Spark Structured Streaming 来实现? 我看到 Jacek L
我想知道:逐 block 读取 jp2 并将数据存储在缓冲区对象中的预期方法是什么? 现在我正在做类似的事情。 /* note I already created stream and configu
我是一名优秀的程序员,十分优秀!