Created
July 5, 2023 17:25
-
-
Save boehs/0fc54a59fff1d84043da341d837c62df to your computer and use it in GitHub Desktop.
OWID helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let global: { | |
[entity: string]: { | |
[year: number]: { | |
[col: string]: string | |
} | |
} | |
} = {} | |
async function pCSV(fName: string) { | |
const f: string[][] = (await Bun.file(fName + ".csv").text()).split("\n").map(l => l.split(',')); | |
const headings = f.shift()! | |
f.forEach((l,i) => { | |
let entity: string, year: string, value: string | |
l.forEach((e,i) => { | |
if (headings[i] == "Entity") entity = e | |
if (headings[i] == "Year") year = e | |
if (headings[i] == fName) value = e | |
if (year == undefined || entity == undefined) return | |
if (global[entity!] == undefined) global[entity!] = {} | |
if (global[entity!][year!] == undefined) global[entity!][year!] = {} | |
global[entity!][year!][fName] = value! | |
}) | |
}) | |
} | |
let wanted = [/*list of column headers*/] | |
for (let f of wanted) { | |
await pCSV(f) | |
} | |
let headings = ["Entity","Year",...wanted] | |
let str = headings.join(',') | |
Object.entries(global).forEach(([country, v]) => { | |
Object.entries(v).forEach(([year, v]) => { | |
let workingStr: string[] = []; | |
workingStr.push(country) | |
workingStr.push(year) | |
wanted.forEach(k => { | |
workingStr.push(v[k]) | |
}) | |
str += "\n" + workingStr.join(',') | |
}) | |
}) | |
Bun.write("output.csv", str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment