|Python| cvs <-> json

From .csv to .json
The format of data.csv:
country, location, school_name,url

I save every row as a dictionary “row = {}”
And “data” is the list of dictionaries.

import json
import csv

data = []
with open('data.csv', newline='') as csvFile:
csvReader = csv.DictReader(csvFile)
for each in csvReader:
 row ={}
 row['country'] = each['country']
 row['location'] = each['location']
 row['name'] = each['name']
 row['url'] = each['url']
 data.append(row)

jsonFilePath = 'saved.json'
with open(jsonFilePath, 'w') as write_file:
 write_file.write(json.dumps(data, indent = 4))

From .json to data structions

The format of saved.json:
// saved.json
[
 {
  ”country”: “US”,
  ”location”: “TX”,
  ”school”: “University of Texas”,
  ”url”: “https://ece.utdallas.edu/&#8221;
 },
 {
  ”country”: “US”,
  ”location”: “CA”,
  ”name”: “University of California”,
  ”url”: “”
 }
]

I put the array as dictionary.
Hence, I pull every array of json_array, then put into tmp_dict.

import json

with open('saved.json', 'r') as input_file:
    json_array = json.load(input_file)

store_dict_as_list = []
for item in json_array:
    tmp_dict = {}
    tmp_dict['country'] = item['country']
    tmp_dict['location'] = item['location']
    tmp_dict['name'] = item['name']
    tmp_dict['url'] = item['url']
    store_dict_as_list.append(tmp_dict)

|Python| Make your Line Bot on Heroku

Making Line bot is very simple.
There are 3 parts.
1. Apply Line Message API
2. Apply Google sheet API (This is not necessary. )
3. Deploy your service on Heroku

I design a robot. Every user can ask some information.
Those information were collected by everyone and wrote on online google sheet.

My Demo LineBot app


There are some Heroku usage:
// After install CIL tool…
heroku login
// If you need to see the log of backend
heroku logs
// If you want to run commands on Heroku
heroku run {some cmd you want to use in backend}
e.g.
heroku run ls
//If you want to do some regular job
https://devcenter.heroku.com/articles/scheduler

Notice:
You can NOT save file as running schedule job.
Heroku is IMMUTABLE.
You can not save file by your process.
 

References:
1. for Line:
https://github.com/yaoandy107/line-bot-tutorial
Line business
2. for Google:
如何透過python建立google表單-使用google-sheet-api
Google console
3. deploy on Heroku
https://github.com/yaoandy107/line-bot-tutorial

WordPress.com.

Up ↑