相关疑难解决方法(0)

如何在Python中逐行读取大文件

我想迭代整个文件的每一行.一种方法是通过读取整个文件,将其保存到列表中,然后浏览感兴趣的行.这种方法使用了大量内存,所以我正在寻找替代方案.

我的代码到目前为止:

for each_line in fileinput.input(input_file):
    do_something(each_line)

    for each_line_again in fileinput.input(input_file):
        do_something(each_line_again)
Run Code Online (Sandbox Code Playgroud)

执行此代码会显示错误消息:device active.

有什么建议?

目的是计算成对的字符串相似性,意味着对于文件中的每一行,我想与每隔一行计算Levenshtein距离.

python file-read

517
推荐指数
6
解决办法
77万
查看次数

在Python中获取文件大小?

是否有内置函数来获取文件对象的大小(以字节为单位)?我看到有些人这样做:

def getSize(fileobject):
    fileobject.seek(0,2) # move the cursor to the end of the file
    size = fileobject.tell()
    return size

file = open('myfile.bin', 'rb')
print getSize(file)
Run Code Online (Sandbox Code Playgroud)

但根据我使用Python的经验,它有很多辅助函数,所以我猜可能有一个内置函数.

python size file

433
推荐指数
5
解决办法
50万
查看次数

使用Python处理大型文件[1000 GB或更多]

假设我有一个1000 GB的文本文件.我需要找出短语在文本中出现的次数.

有没有更快的方法来做我正在使用的人?完成任务需要多少钱.

phrase = "how fast it is"
count = 0
with open('bigfile.txt') as f:
    for line in f:
        count += line.count(phrase)
Run Code Online (Sandbox Code Playgroud)

如果我是对的,如果我没有在内存中的这个文件,我会等到每次我进行搜索时PC加载文件,这应该至少需要4000秒,250 MB /秒的硬盘驱动器和文件10000 GB.

python performance text file python-2.7

16
推荐指数
3
解决办法
7175
查看次数

标签 统计

python ×3

file ×2

file-read ×1

performance ×1

python-2.7 ×1

size ×1

text ×1