What to do: a total of 12 months of JSON data (that is, 12 JSON files), a unit of JSON data is shown below. Read the data and find the annual and quarterly averages for each (over 100) cities.
{
"Time_point": "2014-01",
"Area": "* * *",
"AQI": "71",
"Pm2_5": "47",
"PM10": "69",
"Rank": "7"
}
I do this in two steps, read the JSON and import SQLite, find the database data as needed, and calculate the average. Import the database first the move is somewhat superfluous, but it also allows the data to be handled in an orderly manner.
Traverse a folder 12 JSON files with Os.walk. After parsing the JSON data (for example, j = Json.load (f)), it is found that the JSON data is two-tier, but as long as the dict-like index can be, nothing more than the index two tiers (for example, data = j[' rows '],pm25 = data[i][' pm2_5 '], where I is the number of JSON units traversed). Then import the database, the first part is done.
To take a value from a database and then calculate nothing, it's the value of rows = Conn.execute ("SELECT * * * * * *")The for row in rows is traversed. One exception had to be mentioned when the last average was inserted into the table: "Sqlite3. Programmingerror:you must 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (l Ike text_factory = str). It is highly recommended so instead just switch your application to Unicode strings. " Find the answer in stack overflow:connection =sqlite3.connect(...)
connection.text_factory = str。也没深究,问题解决了。涉及到中文很容易出现这个毛病。
Use Python to read JSON data and calculate the data average