javascript - Build flat table from CSV files -
i have 500 csv files in format:
indicatora_name.csv
1900 1901 1902 ... norway 3 2 sweden 1 3 3 denmark 5 2 3 ...
indicatorb_name.csv
1900 1901 1902 ... norway 1 3 4 sweden 1 2 iceland 1 6 3 ...
- years in columns, countries in rows.
- notice countries, years , values may differ between files.
i'd run through these files , make flat table (csv file) structure:
country, year, indicatora_name, indicatorb_name, ... sweden, 1900, 1, 1 sweden, 1901, 3, 2 norway, 1900, 3, 1 ...
preferably in php or javascript i'm willing learn new.
you should code following code:
$file = file_get_contents('file.csv'); $lines = explode("\n", $file); //lines $years = explode(";", $lines[0]); //first line years, gives array of years for($i = 1, $c = count($lines)-1; $i < $c; ++$i){ //iterate on lines (excluding years) $linedata = explode(';', $lines[$i]); //array line $country = $linedata[0]; //first line entry country unset($linedata[0]); $indicators = $linedata; //and rest indicators query('insert data(country, year, indicatora_name) values(?,?,?)', $country, $year, $indicators[0]); }
Comments
Post a Comment