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 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制