Javascript 从CSV文件构建平面表

Javascript 从CSV文件构建平面表,javascript,php,csv,etl,Javascript,Php,Csv,Etl,我有500个此格式的CSV文件: 指示符a_Name.csv 1900 1901 1902 ... Norway 3 2 Sweden 1 3 3 Denmark 5 2 3 ... 1900 1901 1902 ... Norway 1 3 4 Sweden 1 2 Iceland 1 6 3 ... 1900

我有500个此格式的CSV文件:

指示符a_Name.csv

1900 1901 1902 ... Norway 3 2 Sweden 1 3 3 Denmark 5 2 3 ... 1900 1901 1902 ... Norway 1 3 4 Sweden 1 2 Iceland 1 6 3 ... 1900 1901 1902 ... 挪威3 2 瑞典1 3 3 丹麦5 2 3 ... 指示符号b_Name.csv

1900 1901 1902 ... Norway 3 2 Sweden 1 3 3 Denmark 5 2 3 ... 1900 1901 1902 ... Norway 1 3 4 Sweden 1 2 Iceland 1 6 3 ... 1900 1901 1902 ... 挪威1 3 4 瑞典1 2 冰岛1 6 3 ...
  • 列中的年份,行中的国家
  • 请注意,文件之间的国家、年份和值可能不同
我想浏览所有这些文件并制作一个具有以下结构的平面表格(CSV文件):

country, year, IndicatorA_Name, IndicatorB_Name, ... Sweden, 1900, 1, 1 Sweden, 1901, 3, 2 Norway, 1900, 3, 1 ... 国家、年份、指标名称、指标名称。。。 瑞典,1900,1,1 瑞典,1901年3月2日 挪威,1900,3,1 ... 最好使用PHP或JavaScript,但我愿意学习一些新东西。

使用

$lines = explode(PHP_EOL, $csv);
$data = array();
foreach ($lines as $line)
  $data[] = explode("\t", $line);
(如果它像您的示例中一样是以制表符分隔的)并通过两个循环运行它

编辑 下面是一段经过测试的代码:

$csv1 = <<<TXT
        1900    1901    1902
Norway  3   2   
Sweden  1   3   3
Denmark 5   2   3
TXT;
$csv2 = <<<TXT
        1900    1901    1902
Norway  1   3   4
Sweden  1   2   
Iceland 1   6   3    
TXT;

$csvs = array(
  'IndicatorA_Name' => $csv1,
  'IndicatorB_Name' => $csv2);
/* of course, if you're pulling this from csv files, 
   you need to modify it accordingly, e.g.

$files = array('IndicatorA_Name', 'IndicatorB_Name', ...);
$csvs = array();
foreach ($files as $f)
  $csvs[] = file_get_contents($path . '/' . $f . '.csv');

   or use file(), then you don't need the first `explode` line later */


$data = array();
foreach ($csvs as $indicator => $csv) {
  $lines = explode(PHP_EOL, $csv);

  $header = explode("\t", array_shift($lines));
  foreach ($lines as $line) {
    $fields = explode("\t", $line);

    for ($i = 1; $i < count($fields); $i++) {
      $data[$fields[0]][$header[$i]][$indicator] = $fields[$i];
    }
  }
}

$rows = array();
foreach ($data as $country => $years) {
  foreach ($years as $year => $values) {
    $str = sprintf(PHP_EOL."('%s', '%d'", mysql_real_escape_string($country), intval($year));

    foreach (array_keys($csvs) as $indicator) {
      if (isset($values[$indicator]))
        $str .= sprintf(", '%s'", mysql_real_escape_string(trim($values[$indicator])));
      else
        $str .= ", ''";
    }
    $rows[] = $str . ")";
  }
}

$sql = "INSERT INTO table_name (".implode(',', array_keys($csvs)).") VALUES ".
       implode(',', $rows);

您可能应该编写如下代码:

    $file = file_get_contents('file.csv');
    $lines = explode("\n", $file); //lines
    $years = explode(";", $lines[0]); //first line is years, so it gives us array of years
    for($i = 1, $c = count($lines)-1; $i < $c; ++$i){ //iterate over lines (excluding years)
        $lineData = explode(';', $lines[$i]); //array from line
        $country = $lineData[0]; //first line entry is country
        unset($lineData[0]); 
        $indicators = $lineData; //and the rest are indicators
        query('INSERT INTO data(country, year, IndicatorA_Name) VALUES(?,?,?)', $country, $year, $indicators[0]);
    }
$file=file\u获取内容('file.csv');
$lines=explode(“\n”,$file)//线
$years=爆炸(“;”,$line[0])//第一行是年,所以它给我们一系列的年
对于($i=1,$c=count($line)-1;$i<$c;++$i){//迭代行(不包括年)
$lineData=explode(“;”,$lines[$i]);//从第行开始的数组
$country=$lineData[0];//第一行输入是country
未设置($lineData[0]);
$indicators=$lineData;//其余都是指示符
查询(‘插入数据(国家、年份、指标名称)值(?,?)’,$country、$year、$indicators[0]);
}
我建议使用(参见链接了解用法示例)或(Czetechnology建议使用
“\t”
作为分隔符)


这样,您就可以自动支持边缘情况,如嵌入的分隔符(例如逗号分隔文件中字段中的逗号)。通常最好不要重新发明轮子。

我用经过测试的代码编辑了我的答案。这是为较少的数据库查询而优化的。但是,如果您想在每个循环周期中执行sql插入,您可以节省一些代码。