You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
903 B

  1. import pandas as pd
  2. SEP = " "
  3. NEW_LINE = "\n"
  4. O_OBJ = "{"
  5. C_OBJ = "}"
  6. def main():
  7. df = pd.read_csv("./cities.csv")
  8. uf_to_city = df.groupby(["state_code", "state"])
  9. JSON = "const states = [\n"
  10. for name, _ in uf_to_city:
  11. UF, state_name = name
  12. JSON += "\t\t"+O_OBJ+ " value: \"" +UF+"\", " + "label: \"" + f"{state_name}" + "\" " + C_OBJ + ",\n"
  13. JSON += "];"
  14. JSON += """\n\n\nconst statesToCities = {\n"""
  15. for name, group in uf_to_city:
  16. UF, state_name = name
  17. JSON += "\t" + f"{UF} : " + O_OBJ + "\n\t\t" + "state_name: " + "\""+state_name+"\"," + NEW_LINE + "\t\tcities: [\n"
  18. for city in group.name:
  19. JSON += "\t\t\t"+O_OBJ+ " value: \"" +city+"\", " + "label: \"" + city + "\" " + C_OBJ + ",\n"
  20. JSON += "\t\t]\n\t},\n"
  21. JSON += "\n};"
  22. JSON += "export {states, statesToCities}"
  23. print(JSON)
  24. main()