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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上