读取和解析非常大的文件的内容 [英] Read and parse contents of very large file

查看:122
本文介绍了读取和解析非常大的文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在哪里运行我得到的脚本:

 致命错误:允许内存大小为1895825408字节耗尽(试图分配1029206974字节)... 

我的脚本目前只是:
$ b $ $ $ $ $ $ $ $ $ $ $ $ file_get_contents('allCountries.txt');
$ b $ file = str_replace(array(\r\\\
,\t),array([NEW * LINE],[tAbul * Ator]) $ file);

我把php.ini中的内存限制设置为-1,然后给我:

$ p $ 致命错误:内存不足

是否有部分打开文件,然后移动到下一个部分,以减少内存一次用完?

解决方案

是的,您可以逐行阅读:

  $ handle = @fopen(/ tmp / inputfile.txt,r); 
if($ handle){
while(($ buffer = fgets($ handle,4096))!== false){
echo $ buffer;
}
fclose($ handle);
}


I am trying to parse a tab delimited file that is ~1GB in size.

Where I run the script i get:

Fatal error: Allowed memory size of 1895825408 bytes exhausted  (tried to allocate 1029206974 bytes) ...

My script at the moment is just:

$file = file_get_contents('allCountries.txt') ;

$file = str_replace(array("\r\n", "\t"), array("[NEW*LINE]", "[tAbul*Ator]"), $file) ;

I have set the memory limit in php.ini to -1, which then gives me:

Fatal error: Out of memory (allocated 1029963776) (tried to allocate 1029206974 bytes)

Is there anyway to partially open the file and then move on to the next part so less memory is used up at one time?

解决方案

Yes, you can read it line by line:

$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    fclose($handle);
}

这篇关于读取和解析非常大的文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆