Skip to content

Instantly share code, notes, and snippets.

@elibroftw
Last active March 27, 2022 17:50
Show Gist options
  • Save elibroftw/b9ec2af8fd370d36acd42dbb244ac3fa to your computer and use it in GitHub Desktop.
Save elibroftw/b9ec2af8fd370d36acd42dbb244ac3fa to your computer and use it in GitHub Desktop.
Excel file (xlsx) to Javascript array
import pandas as pd
import openpyxl
df = pd.read_excel(r'C:\Users\maste\Downloads\countries.xlsx')
javascript_1 = 'export const countries = [\n'
javascript_2 = 'export const countries_fr = [\n'
for _, row in df.iterrows():
row['EN'] = row['EN'].replace('’', "'")
row['FR'] = row['FR'].replace('’', "'")
quote = '"' if "'" in row['EN'] else "'"
javascript_1 += f"('{row['Codes']}', {quote}{row['EN']}{quote}),\n"
quote = '"' if "'" in row['FR'] else "'"
javascript_2 += f"{quote}{row['FR']}{quote},\n"
javascript_1 += '];\n'
javascript_2 += '];'
with open('countries.js', 'w', encoding='utf-8') as f:
f.writelines((javascript_1, javascript_2))
# countries = [ (code, name), (code, name), ... ]
# countries_fr = [french_names]
# then i18n can just iterate both and match them up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment