dongni3854 2014-01-06 03:44
浏览 20
已采纳

每n行拆分文件[关闭]

I’m working with the royal mail PAF database in csv format (approx 29 million lines), and need to split the data into files of approx 1000 lines.

I found this solution to write the files, but do not know how to a. open the file, and b. tell the script to delete the lines from the original file after copying them.

Can anyone advise?

  • 写回答

2条回答 默认 最新

  • dongyouzhi7218 2014-01-06 03:49
    关注

    I don't know the royal PAF database, but you open files with fopen(), read a line with fgets() and delete files with unlink().

    Your found solution shows the idea of splitting every 1000 lines, but in your case there is no need for calling any csv function at all. It's just a simple "copy each 1000 lines into new file".

    $bigFile = fopen("paf.csv", "r");
    $j = 0;
    
    while(! feof($bigFile)) {
        $smallFile = fopen("small$j.csv", "w");
        $j++;
    
        for ($i = 0; $i < 1000 && ! feof($bigFile); $i++) {
            fwrite($smallFile, fgets($bigFile));
    
        }
        fclose($smallFile);
    
    }
    fclose($bigFile);
    unlink("paf.csv");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害