Use Python to read JSON data and calculate the data average

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.