1. 程式人生 > 其它 >Python讀取json檔案,並轉化為字典進行提取欄位(出現索引must be int,not str)解決方案

Python讀取json檔案,並轉化為字典進行提取欄位(出現索引must be int,not str)解決方案

Python讀取json檔案,並轉化為字典進行提取欄位(出現索引must be int,not str)解決方案

def craw_file():
    di_review_keyword=[]
    for i in range(10):
        with open('E:python_py景區詳情\test\'+'北京'+'_'+str(0)+'_'+str(1)+'.json','r') as file:
            #d讀取檔案,並轉換成json格式
            html=file.read()
            # html_json=json.loads(html)   #以json形式載入
            #
            # print(type(html),type(html_json))
            html_dict=eval(html)  #將其轉化為字典並進行提取(一般出現索引must be int,not str)
            print(type(html_dict),html_dict)
            html_result=html_dict["result"]
            #基本資訊
            name=html_result["name"]
            address=html_result["address"]