douhu7807 2013-09-23 20:06
浏览 45
已采纳

PHP中的CSV解析条件

I created a CSV parser that works fine for some CSV files I've found online, but one that I converted from XLS to CSV via Microsoft Excel 2011 does not work. The ones that work are formatted as such:

"Sort Order","Common Name","Formal Name","Type","Sub Type","Sovereignty","Capital","ISO 4217 Currency Code","ISO 4217 Currency Name","ITU-T Telephone Code","ISO 3166-1 2 Letter Code","ISO 3166-1 3 Letter Code","ISO 3166-1 Number","IANA Country Code TLD" "1","Afghanistan","Islamic State of Afghanistan","Independent State",,,"Kabul","AFN","Afghani","+93","AF","AFG","004",".af".........................etc...

The one that doesn't work is formatted like this:

Order Id,Date Ordered,Date Returned,Product Id,Description,Order Reason Code,Return Qty,Order Return Comment,Ship To Name,Ship To Address1,Ship To Address2,Ship To Address3,Ship To City,Ship To State,Ship To Zipcode,Ship To Country,Disposition,Ship To Email,ShipVia 5555555,2013-07-05 13:58:36.000,2013-08-16 00:00:00.000,5555-55,0555 - Some Test Thing,Refund,2,,jeric beatty,123 fake st,,,burke,NJ,55055,US,Discard,test@test.com,Super Fast Shipping

Is there anyway to get excel to export in the format as the first one? I would like to avoid doing this manually as the file is huge and I would have to manually edit lots of parts of it where I couldn't do a "replace all". Another issue could be that there are double and sometimes triple commas in some places. Though this does appear in both files.

Here is the parser:

    function ingest_csv() {

  $file_url = 'http://www.path.to/csv/file.csv';
  $record_num = 0;
  $records = array();
  $header = array();
  if (($handle = fopen($file_url, "r")) !== FALSE) {
    $records['id'] = '';
    while (($data = fgetcsv($handle)) !== FALSE) {
      $records['id'][$record_num] = '';
      $cell_num = 0;
      foreach ($data as $cell) {
        if($record_num == 0) {
          $header = $data;
        } else {
          $current_key = $header[$cell_num];
          $records['id'][$record_num][$current_key] = $cell;
        }
        $cell_num++;
      }
      $record_num++;

    }

    fclose($handle);
  }
  else {
    echo 'could not open file.';
  }
  return array($record_num, $records);
}

function batch_csv() {
  list($num_rows, $rows) = ingest_csv
  print_r($num_rows);
  print_r($rows);
}
  • 写回答

1条回答 默认 最新

  • douxian1923 2013-09-23 22:09
    关注

    As mentioned in the comments though you may be trying to reinvent the wheel here, though personally I've asked questions where I didn't want to give long rambling explanations of why I was forced to use unconventional approaches so should this be one of those situations here's an answer.

    In OpenOffice Calculator (for example) and when you go to save as CSV you get a number of further options including the decision to double quote all fields.

    Unfortunately Excel doesn't give you the choice, but Microsoft do offer up a workaround using a macro - http://support.microsoft.com/kb/291296/en-us

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line