dongyi8416 2014-05-14 20:41
浏览 55

我得到的csv结构不正确[重复]

This question already has an answer here:

I'm trying to create a new CSV from another with PHP .

The results i get is what i need, except the organisation of the CSV .

I don't want a header and i need to separate the fields by column.

I want to get something like this :

      A           B      C
T4004XDUHF210   14.55   40

I'm getting :

          A
article_id,2.50,availability

T4004XDUHF210,14.55,40

MSME10/1-ZS10,14.64,58

R3506XKIKT501,14.86,49

T3506XDUHF207,14.86,40

R4103504XKIKT602,14.86,40

My PHP Code :

$outputfp = fopen('new.csv', 'w');

if (($inputfp = fopen("temp/tmp/138702_fr.csv", "r")) !== FALSE) {

while (($data = fgetcsv($inputfp, 1000, "|")) !== FALSE) {

        $line = array();
        // SKU
        $line[]=$data[1];
        // Price
        $price_calcul=(($data[4]*0.20)+$data[4])+2.5;
        $line[] = number_format($price_calcul,2); 
        // Quantity
        $line[]=$data[9];

        fputcsv($outputfp, $line);

        }

    // Clean up
    fclose($inputfp);
    fclose($outputfp);
}

So i don't need article_id,2.50,availability and i want tabs delimited.

</div>
  • 写回答

1条回答 默认 最新

  • douyou2234 2014-05-14 21:41
    关注

    I've noted the changes you need to make below:

    <?php
    $outputfp = fopen('new.csv', 'w');
    
    if (($inputfp = fopen("temp/tmp/138702_fr.csv", "r")) !== FALSE) {
        $i = 0;            // <-- Add this line
        while (($data = fgetcsv($inputfp, 1000, "|")) !== FALSE) {
            $i++;          // <-- Add this line
    
            if ($i == 1) { //
                continue;  // <-- Add these lines
            }              //
    
            $line = array();
            // SKU
            $line[]=$data[1];
            // Price
            $price_calcul=(($data[4]*0.20)+$data[4])+2.5;
            $line[] = number_format($price_calcul,2); 
            // Quantity
            $line[]=$data[9];
    
            fputcsv($outputfp, $line, "\t"); // <-- Change this line
        }
    
        // Clean up
        fclose($inputfp);
        fclose($outputfp);
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大