Skip to content

Instantly share code, notes, and snippets.

@pwfcurry
Created March 3, 2025 15:04
Show Gist options
  • Save pwfcurry/937563a070e603ae80037de1399d12c4 to your computer and use it in GitHub Desktop.
Save pwfcurry/937563a070e603ae80037de1399d12c4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env deno run --allow-read
/**
* Usage:
* ./add_descriptions.ts sic_codes.csv codes.csv
*
* sic_codes.csv should contain two columns:
* code,description
*
* codes.csv should contain a list of codes, one per row.
*/
// Lookup list retrieved from https://www.gov.uk/government/publications/standard-industrial-classification-of-economic-activities-sic
import {parse} from "https://deno.land/[email protected]/csv/mod.ts";
// Ensure we have the right command-line arguments.
if (Deno.args.length < 2) {
console.error("Usage: ./add_descriptions.ts <sic_codes_with_descriptions.csv> <codes.csv>");
Deno.exit(1);
}
const [sicCsvPath, codesCsvPath] = Deno.args;
// Read and parse the SIC codes CSV file.
const sicCsvData = await Deno.readTextFile(sicCsvPath);
const sicRows = await parse(sicCsvData, {skipFirstRow: false}) as Array<Array<string>>;
// Build a map of code to description.
// Assuming the first column is the code and the second column is the description.
const sicMap: Record<string, string> = {};
for (const row of sicRows) {
if (row.length >= 2) {
const code = row[0].trim();
const description = row[1].trim();
sicMap[code] = description;
}
}
// Read and parse the codes CSV file.
const codesCsvData = await Deno.readTextFile(codesCsvPath);
const codeRows = await parse(codesCsvData, {skipFirstRow: false}) as Array<Array<string>>;
// Process each code and print out the code with its description.
for (const row of codeRows) {
// Expecting each row to contain only the code in the first column.
const code = row[0].trim();
const description = sicMap[code] || "Description not found";
console.log(`${code},"${description}"`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment