在python中将文本文件分成更小的块

编程入门 行业动态 更新时间:2024-10-28 17:13:22
本文介绍了在python中将文本文件分成更小的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个文本文件(以 MB 为单位),我想将其分成多个块(以 KB 为单位).我正在模拟网络上的文件传输行为.到目前为止,我能够根据用户输入的行数(由 '\n' 分隔)制作块

I have a text file (in MBs) and I want to break it into chunks(in KBs). I am simulating the file transfer behaviour over a network. So far I was able to make chunks according to the number of lines(seprated by '\n') inputed by user like this

def make_chunks(fname): ifile = file(fname,'rb') file_iter = iter(ifile) args = [file_iter] * 10 # No of lines you want to have in one chunk chunks = list(izip_longest(fillvalue = None, *args))

但是块现在大小不同.我如何制作相同大小的块(比如 4KB)

But the chunks are now of different sizes.How would I make chunks of equal size(say 4KB)

推荐答案

您可以按实际字节大小分块:

You can chunk by actual bytesize:

def chunk(fname): with open(fname, 'rb') as fin: return list(iter(lambda: fin.read(4096), ''))

请注意,您最好yield每个块而不是构建列表,让调用者决定是否要构建列表.

Note that you might as well yield each chunk instead of building a list, and let the caller decide if it wants to build a list instead.

for chunk in iter(lambda: fin.read(4096), ''): yield chunk

更多推荐

在python中将文本文件分成更小的块

本文发布于:2023-10-30 00:13:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   文本文件   更小   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!