dsiimyoc804955 2015-02-03 20:22 采纳率: 100%
浏览 45
已采纳

通过php排序csv数据

I have a .csv file on my server that looks like this :

C:/Users/Server/Pictures/P1040805.JPG,0.163533
C:/Users/Server/Pictures/P1040808.JPG,0.170159
C:/Users/Server/Pictures/P1040819.JPG,0.885534
C:/Users/Server/Pictures/P1040825.JPG,0.11393
C:/Users/Server/Pictures/P1040826.JPG,0.0536503

and so on. I found a php script that displays these results in my browser which looks like this:

<?php
$row = 1;
if (($handle = fopen("C:/xampp/htdocs/results/result.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo " ","Record #",$row," "; $row++;
        for ($recordcount=0; $recordcount < $num; $recordcount++) {
            echo $data[$recordcount] . " 
";
        }
    }
    fclose($handle);
} ?>

But when it shows the csv, the layout is totally messed up, e.g several results in one line.

I'd like to do three things -

-convert the 0.xx to a percentage -put the percentage value in the first column and the path in the second -and most of all, simply have the data stacked neatly so that every line has one result.

Can that all be done in php? and could somebody tell me what parameter I have to tweak to get one result per line and not more?

  • 写回答

1条回答 默认 最新

  • dongyin2643 2015-02-04 01:46
    关注

    isn't that hard, check here

    if (($handle = fopen("yourfile.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
                $percentaje=$data[1]*100;
                echo $percentaje."%&nbsp".$data[0]. "<br>";
        }
        fclose($handle);
    } 
    

    result:

    16.3533% C:/Users/Server/Pictures/P1040805.JPG
    17.0159% C:/Users/Server/Pictures/P1040808.JPG
    88.5534% C:/Users/Server/Pictures/P1040819.JPG
    11.393% C:/Users/Server/Pictures/P1040825.JPG
    5.36503% C:/Users/Server/Pictures/P1040826.JPG
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存