Chartjs-Convert csv to json for javascript plotting library chartjs in Python or PHP

1👍

The high level process flow I would suggest is:

  • read csv
  • convert to python dict/list data structure
  • convert data structure to json string
  • send the json string to your javascript

for help with reading the csv you can have a look at the documentation https://docs.python.org/2/library/csv.html

Once you have data build your structure something like:

list_of_day_oranges_pairs = [('Monday', 314) ....]  # getting to this format should be easy 
data = [{'day': day, 'oranges': oranges} for (day, oranges) in list_of_day_oranges_pairs]

Then convert the data to json:

data_string = json.dumps(data)

Now that data_string is ready to be sent to your javascript

Leave a comment