简体   繁体   English

删除CSV行

[英]Delete CSV Rows

How would I go about deleting a specific number of rows from a large csv file? 如何从大型csv文件中删除特定数量的行? ie, I am looking to delete the first 100 rows from a file containing over 200,000 rows. 即,我正在寻找从包含200,000行的文件中删除前100行。 Just curious to see if there is another way to handle things other than completely re-writing the file to a temp file and back to overwrite the original file. 只是想知道是否还有其他处理方式,除了将文件完全重写为临时文件并返回以覆盖原始文件外。

Thanks in advance for any help/suggestions. 在此先感谢您的帮助/建议。

Does it need to be in php? 是否需要在php中?

You can use the unix command tail to print every line in a file starting from the nth line: 您可以使用unix命令tail从第n行开始打印文件中的每一行:

tail -n +101 file.csv > trimmed.csv

Or using PHP: 或使用PHP:

$lines = file('file.csv'); // read file as array of lines $trimmed = array_slice($lines, 100); // slice array at index file_put_contents('trimmed.csv', implode($trimmed)); // implode and write to file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM