将文本文件读取,排序和拆分为块

时间:2016-11-02 18:35:28

标签: php

我正在尝试为以下任务重新激活我的PHP知识: 我有一个更大的文本文件,其中包含未分隔的逗号分隔信息行,每个值都用'“'括起来。 每一行都可以理解为一个数据集,该行的第一个值告诉我该行属于哪个表。

现在我需要读取文件,对行进行排序(以便属于同一个表的行在一起),检测不同的块并将它们保存在单独的文本文件中。之后,我可以使用来自infile的加载数据快速导入到mysql数据库中。

所以,我可以通过这个打开文件并对行进行排序:

<?php
$lines = file("importfile_unsorted.txt");
natsort($lines);
file_put_contents("importfile_sorted.txt", implode($lines));
?>

这很有效。但现在我被困了。 importfile_sorted.txt如下所示:

"AV1","0","0","0","0","0","0","0","0","0","0","0:0","0:0","0:0"
"AV2","0","0","0","0","0","0","0","0","0","0","0:0","0:0","0:0"

.... [这将是第一个块,所有这些行都应保存在“av.txt”中 在下一行中,新块“F”以几行开头:

"F1","D","D","Deutsch",,,"0","W"
"F4","E","E","Englisch",,,"0","W"
"F7","K","K","Kath.Religionslehre",,,"0","W"
"F8","Ev","Ev","Evang.Religionslehre",,,"0","W"
"F9","Eth","Eth","Ethik",,,"0","W"

... [将所有这些以Fxx开头的行保存到文件f.txt并转到下一个块]

"G1","PhL","PÜG"
"G2","ChL","ChÜ"

...

"K1","5a","5a",,"304","Ma","Wei","0","16","16","5",,,,,"1","1","0",,"0","0","0","0"
"K2","5b","5b",,"303","Wo","Hm","0","32","16","5",,,,,"1","1","0",,"0","0","0","0"
"K3","5c","5c",,"302","Gr","Ro","0","32","16","5",,,,,"1","1","0",,"0","0","0","0"

......等等。之后,有一些块具有固定的第一列,如下所示:

"PL","Di 1","Ba","Q12","Inf1","CoR1"
"PL","Di 1","Bb","Q12","F","Ü2"
"PL","Di 1","Eg","Q12","L","M23"

... 和

"PLS","Di 1","Am"," frei "
"PLS","Di 1","Bad"," ----"
"PLS","Di 1","Bk"," frei "

... 其次是几个其他区块(L1 ... L97,M,R1 ... R40,U1 ... U560)。

我知道块的所有可能的“标识符”(AVx,Fx,Gx,.. PL,PLS ..),但也可能省略了一个块,输入文件也没有单行一点都不 输入文件总共包含大约4000行,因此性能不应太低(虽然它不是时间关键的,导入一年可能会完成10次......)。

那么,有没有办法以“智能”和快速的方式完成这项工作,或者我应该逐行读取输入文件,检测并记住第一个值,将当前行添加到结果字符串并循环直到出现新的第一个值?

感谢您的帮助! 的Heiko

1 个答案:

答案 0 :(得分:0)

使用内置的CSV解析器,不要手动拆分 http://php.net/manual/en/function.str-getcsv.php