$line_index += 1; if ($line_index < 2) { $column_names = $current_parts; continue; } $row = array(); $column_index = 0; foreach ($column_names as $column_name) { if (isset($current_parts[$column_index])) { $row[$column_name] = $current_parts[$column_index]; } else { $row[$column_name] = null; } $column_index += 1; } if ($line_index > 2) { fwrite($output_file_handle, ",\n"); } fwrite($output_file_handle, json_encode($row)); } fwrite($output_file_handle, "];\n"); fclose($file_handle); fclose($output_file_handle); } $cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The input CSV file', 'default' => 'php://stdin'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the converted JSON data to', 'default' => 'php://stdout'), 'arrayname' => array('short' => 'a', 'type' => 'required', 'description' => 'The name of the variable to assign the data array to')); $options = cliargs_get_options($cliargs); $input_file = $options['inputfile']; $output_file = $options['outputfile']; $array_name = $options['arrayname']; process_csv_file($input_file, $output_file, $array_name, ',');
global $zip_code_state_ranges; $file_handle = fopen($file_name, "r") or die("Couldn't open {$file_name}\n"); $output_file_handle = fopen($output_file_name, "w") or die("Couldn't open {$output_file_name}\n"); $line_index = 0; while (!feof($file_handle)) { $current_parts = fgetcsv($file_handle, 0); $naked_zip = $current_parts[$zip_column]; $full_zip = ''; foreach ($zip_code_state_ranges as $state => $ranges) { foreach ($ranges as $range) { $start = $range[0]; $end = $range[1]; if ($naked_zip >= $start && $naked_zip <= $end) { $full_zip = $state . " " . $naked_zip; break; } } } $current_parts[$zip_column] = $full_zip; fputcsv($output_file_handle, $current_parts); } fclose($file_handle); fclose($output_file_handle); } $cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The input CSV file', 'default' => 'php://stdin'), 'outputfile' => array('short' => 'o', 'type' => 'optional', 'description' => 'The file to write the converted JSON data to', 'default' => 'php://stdout'), 'zip_column' => array('short' => 'z', 'type' => 'optional', 'description' => 'The name of the variable to assign the data array to', 'default' => '1')); $options = cliargs_get_options($cliargs); $input_file = $options['inputfile']; $output_file = $options['outputfile']; $zip_column = $options['zip_column']; process_csv_file($input_file, $output_file, $zip_column);
{ if (empty($row[SERIES_NAME_STRING]) || empty($row[COUNTRY_CODE_STRING])) { return; } $series_name = $row[SERIES_NAME_STRING]; $country_code = $row[COUNTRY_CODE_STRING]; $series_name = str_replace('/', '\\', $series_name); $file_name = $output_folder . $series_name; $file_already_exists = file_exists($file_name); $file_handle = fopen($file_name, 'a') or die("Couldn't open {$file_name} for appending"); if (!$file_already_exists) { fputcsv($file_handle, array('country_code', 'time', 'value')); } foreach ($row as $column_name => $column_value) { if (is_numeric($column_name)) { $year = $column_name; $value = $column_value; if ($value === '') { continue; } fputcsv($file_handle, array($country_code, $year, $value)); } } fclose($file_handle); } $cliargs = array('inputfile' => array('short' => 'i', 'type' => 'optional', 'description' => 'The World Bank data file holding all the series in CSV format', 'default' => 'php://stdin'), 'outputfolder' => array('short' => 'o', 'type' => 'required', 'description' => 'The directory to write out all the individual data files to')); $options = cliargs_get_options($cliargs); $input_file = $options['inputfile']; $output_folder = $options['outputfolder']; process_csv_file($input_file, $output_folder, ',');