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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵