使用PHP合并两个大型CSV文件 [英] Merge two large CSV files with PHP

查看:153
本文介绍了使用PHP合并两个大型CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个大型CSV文件与PHP合并。这些文件太大,甚至不能一次放入内存。在伪代码中,我可以想到这样的东西:

I want to merge two large CSV files with PHP. This files are too big to even put into memory all at once. In pseudocode, I can think of something like this:

for i in file1
  file3.write(file1.line(i) + ',' + file2.line(i))
end

当我使用 fgetcsv 循环文件时,我不清楚我将如何从一个确定的 n

But when I'm looping through a file using fgetcsv, it's not really clear how I would grab line n from a certain file without loading the whole thing into memory first.

任何想法?

编辑:我忘了提及这两个文件中的每一个具有相同的行数,并且它们具有一对一的关系。也就是说,file1中的第62,324行与file2中的第62,324行相同。

I forgot to mention that each of the two files has the same number of lines and they have a one-to-one relationship. That is, line 62,324 in file1 goes with line 62,324 in file2.

推荐答案

不知道您使用的是什么操作系统,如果您使用的是Linux,请使用 粘贴 命令可能比尝试在PHP中更容易。

Not sure what operating system you're on, but if you're using Linux, using the paste command is probably a lot easier than trying to do this in PHP.

如果这是一个可行的解决方案,你不必绝对需要PHP,你可以尝试以下:

If this is a viable solution and you don't absolutely need to do it in PHP, you could try the following:

paste -d ',' file1 file2 > combined_file

这篇关于使用PHP合并两个大型CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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