解析php上的大型csv文件

时间:2016-11-22 15:57:30

标签: php csv parsing

我有一个csv文件,它是制表符分隔的。在单元格内部使用分号作为值之间的分隔符。 php是否意味着处理文件并到达每行值数组右侧的出口。

这是我的代码:

while (cont) {

  // getInt() is the troublemaker, so we try it:
  // Notice I have changed 'number' to 'integer' here - this improves
  // UX by prompting the user for the data type the program expects:

  try {
    printb(convert(getInt(in, "Enter an integer: ")));
  }

  // we catch the Exception and name it. 'e' is a convention but you
  // could call it something else. Sometimes we will use it for info,
  // and in this case we don't really need it, but Java expects it
  // nonetheless.
  // We do our error handling here: (notice the call to in.next() -
  // this ensures that the string that was entered gets properly
  // handled and doesn't cascade down to the assignment of 'a') - if
  // this confuses you, try it without the in.next() and see what
  // happens!

  catch (Exception e) {
    System.out.println("\nPlease enter an integer.\n");
    in.next();
  }

  // Again, in this case, the finally isn't necessary, but it can be
  // very handy, so I'm using it for illustrative purposes:

  finally {
    System.out.println("Do you want to continue (yes or no)?");
    a = in.next();
    if (a.equals("yes")) {
      cont = true;
    } else {
      System.out.println("You answered no. Have a nice day.");
      cont = false;
    }
  }
}

输出数组:

$handle = fopen($name_csv, "r");
            $data_import = array();
            if(empty($handle) === false) {
                while(($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
                    foreach( $data as $key => $val ) {

                        $data[$key] = trim( $data[$key] );
                        $data[$key] = iconv('windows-1251', 'UTF-8', $data[$key]);
                        $data[$key] = str_replace('""', '"', $data[$key]);
                        $data[$key] = preg_replace("/^\"(.*)\"$/sim", "$1", $data[$key]);

                    }
                    $data_import[] = $data;
                }
                fclose($handle);
            }

文件大小为300 MB。

原始csv:

Array
(
    [0] => Array
        (
            [0] => test;test;test;"test value; test value";test
        )

0 个答案:

没有答案