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 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥15 寻找能匹配的液晶显示屏。