Python 快速學習筆記本 -2

下面幾段是Python 資料型態及基本語法的介紹:

Python 變數與資料型態 By 彭彭



     有關 List 與Tuple 想要快速了解,可以看這一篇文章:


     有 Tuple 範例:



    有關 Python 字典 這篇有更詳細的說明
    

下面幾段是Python 一般流程的控制




下面幾段是Python 函式'、模組、封包




    模組載入的不同語法;


下面幾段是Python 類別、實體物件 




下面幾段是Python 的應用


    針對文字檔案處理,這篇有更詳細的說明:




    快速記憶 讀取網頁,編碼為 "utf-8"
    import urllib.request as request
    src="http://www.**"
    with request.urlopen(src) as response:
        data=response.read().decode("utf-8")
    print(data)

    這篇文章中有處理 URLError 的標準寫法

    如果使用
    data=response.read().decode("utf-8") 出現 0x0A 相關錯誤 請改用
    data=response.read().decode("utf-8","ignore")

    JSON encoder and decoder JSON 說明


    示範:獨立書店(格式 JSON) 
 主要欄位說明:groupTypeName(群組類別)、mainTypeName(主類別)、                mainTypePK(mainTypePK)、name(名稱)、name_eng(名稱英文)、representImage(代表圖像)、intro(簡介)、intro_eng(簡介英文)、 areaCode(郵遞區號)、cityName(縣市)、address(地址)、longitude(經度)、latitude(緯度)、hitRate(點閱數)
    
    測試程式:印出獨立書店店名

import urllib.request as request
import json
src="https://cloud.culture.tw/frontsite/trans/emapOpenDataAction.do?method=exportEmapJson&typeId=M"
with request.urlopen(src) as response:
    data=response.read().decode("utf-8")
independent_bookstore=json.loads(data)
for i in range(len(independent_bookstore)):
 print(independent_bookstore[i]['name'])

    很多JSON 檔案 在網上是用 gzip 的格式儲存
          資料來源為台北市政府公車資料

import urllib.request as request
import json
import gzip
src="https://tcgbusfs.blob.core.windows.net/blobbus/GetStopLocation.gz"
response=request.urlopen(src)
response_data=gzip.decompress(response.read())
BusStop=json.loads(response_data.decode('utf-8'))
for i in range(len(BusStop['BusInfo'])):
 print(BusStop['BusInfo'][i]['name'])







留言

這個網誌中的熱門文章

Arduino 模擬 Modbus Slave

Arduino IDE 1.6.5 + BH1750 + CD74HC4067 多工器

【輕鬆工作家】使用 3D 印表機 製作一台 Arduino CNC GRBL 繪圖機