Skip to content

Instantly share code, notes, and snippets.

@brycefisher
Created March 27, 2014 04:30
Show Gist options
  • Save brycefisher/9800226 to your computer and use it in GitHub Desktop.
Save brycefisher/9800226 to your computer and use it in GitHub Desktop.
Extracts a several of fields that were put into the same cell and outputs a CSV
<?php
// Open the munged file
$in_fp = fopen('CarenChengWordList.csv','r');
while ($row = fgetcsv($in_fp)) {
// Separate out the data in the 3rd column into 3 fields
list($messy_fields, $sentence) = explode("\n", $row[2]);
$period_index = stripos($messy_fields, '.');
$part_of_speech = substr($messy_fields, 0, $period_index);
$definition = substr($messy_fields, $period_index + 1);
// Return structured data
$final_row = array(
trim($row[0]),
trim($row[1]),
trim($part_of_speech),
trim($definition),
trim($sentence),
);
$data[] = $final_row;
}
fclose($in_fp);
// Output as a CSV file
$out_fp = fopen('FIXED-CarenChengWordList-1.csv','w');
fputcsv($out_fp, array('Num', 'Word', 'Part', 'Defintion', 'Sentence'));
foreach ($data as $row)
fputcsv($out_fp, $row);
fclose($out_fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment