Last active
March 27, 2022 17:50
-
-
Save elibroftw/b9ec2af8fd370d36acd42dbb244ac3fa to your computer and use it in GitHub Desktop.
Excel file (xlsx) to Javascript array
This file contains hidden or 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
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