我正在尝试在python中打印出我的json文件,但是由于某些原因,json.load函数使我不断回火。
这是我的json文件:
{“ _ index”:“事件实体生产”,“ _ type”:“事件实体”,“ _ id”:“ AWZShT9ng6suonapWC9v”,“ _ score”:1,“ _ source”:{“ created_at”:“ 2018-10-08T07:12:36.192Z“,” updated_at“:” 2018-10-08T07:12:36.195Z“,” _ created_at“:null,” _ updated_at“:”“ 2018-10-08T07:12:36.196+ 00:00“,” country“:”印度“,” country_code_2“:” IN“,” country_code_3“:” IND“,”大陆“:”亚洲“,”区域“:”马哈拉施特拉邦“,”城市“:”孟买“,” ip“:” 111.119.214.164“,” zip“:null,” lat“:19.469999313354492,” lng“:72.80000305175781,” timezone“:null,” browser“:” Mobile Safari“,” browser_version“: “ 12.0”,“ browser_version_visible”:“ Mobile Safari 12.0”,“ os”:“ iOS”,“ os_version”:“ 12.0”,“ os_version_visible”:“ iOS 12.0”,“ document_title”:null,“ screen_name”:null ,“ session_id”:“ SESSION-ab8c1f814bdb737fa92c65a26bad55312529fcf5”,“ app_name”:null,“ app_package”:null,“ app_version”:null,“ device”:“ iPhone”,“ device_id”:null,“ device_type”:“智能手机” ,“ device_brand”:“ Apple”,“ model”:null,“ manufacturer”:null,“ viewport_width”:320,“ viewport_height”:568,“ viewport_dimensions”:“ 320x568”,“ sdk_ve rsion“:” 0.5.0“,” api_version“:null,” language“:null,” page_title“:” NA“,” referrer“:” https://www.mistay.in/hotels-in-mumbai/?checkin_date=2018-10-09&checkin_slot=1&slot_count=1&guest_count=2&room_count=1“,” referrer_domain“:” www.mistay。 in“,” referrer_path“:” /-孟买酒店/“,” referrer_query“:” checkin_date = 2018-10-09&checkin_slot = 1&slot_count = 1&guest_count = 2&room_count = 1“,” referrer_protocol“:” https“,” referrer_type“ :“ direct”,“ referrer_keyword”:null,“ referrer_type_name”:null,“ referrer_keyword_position”:null,“ source”:“ web-sdk”,“ current_url_domain”:“ www.mistay.in”,“ current_url_path”:“ / hotels-in-mumbai / hotel-sai-palace-inn /“,” current_url_protocol“:” https“,” current_url_query“:” checkin_slot = 1&checkin_date = 2018-10-09&guest_count = 2&slot_count = 1&room_count = 1“,” current_url“ :“ https://www.mistay.in/hotels-in-mumbai/hotel-sai-palace-inn/?checkin_slot=1&checkin_date=2018-10-09&guest_count=2&slot_count=1&room_count=1”,“ event_ts”:“ 2018-10-08T07:12:36.192 + 00:00”,“ is_anonymous_user”:null,“ is_known_user”:null,“ traffic_source”:null,“ traffic_source_type” :null,“ utm_source”:null,“ utm_medium”:null,“ utm_account”:null,“ utm_keyword”:null,“ utm_content”:null,“ utm_campaign”:null,“ profile_id”:“ e13488787a1512e635e28e9c9c9a6e6pro4a9e8pro6a9e6a9e8a8e8c0e7e8c0e0e0c0e0e0e0e0e0e0e0e0e0e0e0e0c0e0e0e le_list_id“:4,” profile_key“:null,” uuid“:” 135ed264-3111-4b1d-a0a2-ba7e7faacb2d“,” event_name“:” visited_hotel“,” account_id“:4,” event_list_id“:13,” __ from_web“ :null,“ __ from_upload”:null,“ __ file_upload_id”:null,“ __ from_ios”:null,“ __ from_android”:null,“ __ from_device_id”:null,“ __ client_ts”:null,“ __ is_uploaded”:null,“ __ uploaded_ts”:null ,“ json_0”:{},“ json_1”:{},“ json_2”:{},“ json_3”:{},“ json_4”:{},“ json_5”:{},“ json_6”:{}, “ json_7”:{},“ json_8”:{},“ json_9”:{},“ json_10”:{},“ json_11”:{},“ json_12”:{},“ json_13”:{},“ json_14“:{},” json_15“:{},” json_16“:{},” json_17“:{},” json_18“:{},” json_19“:{},” json_20“:{},” json_21 “:{},” json_22“:{},” json_23“:{},” json_24“:{},” json_25“:{},” json_26“:{},” integer_0“:4,” integer_1“: 4,“ integer_2”:null,“ integer_3”:null,“ integer_4”:null,“ integer_5”:null,“ integer_6”:null,“ integer_7”:null,“ integer_8”:null,“ integer_9”:null, “ integer_10”:null,“ integer_11”:null,“ integer_12”:null,“ integer_13”:null,“ integer_14”:null,“ integer_15”:null,“ integer_16”:null,“ integer_17”:null,“ integer_18 “:null” teger_19“:null,” integer_20“:null,” integer_21“:null,” integer_22“:null,” integer_23“:null,” integer_24“:null,” integer_25“:null,” integer_26“:null,” float_0“ :null,“ float_1”:null,“ float_2”:null,“ float_3”:null,“ float_4”:null,“ float_5”:null,“ float_6”:null,“ float_7”:null,“ float_8”:null ,“ float_9”:null,“ float_10”:null,“ float_11”:null,“ float_12”:null,“ float_13”:null,“ float_14”:null,“ float_15”:null,“ float_16”:null,“ float_17“:null,” float_18“:null,” float_19“:null,” float_20“:null,” float_21“:null,” float_22“:null,” float_23“:null,” float_24“:null,” float_25“ :null,“ float_26”:null,“ datetime_0”:“ 2018-10-09T00:00:00.000 + 00:00”,“ datetime_1”:null,“ datetime_2”:null,“ datetime_3”:null,“ datetime_4” :null,“ datetime_5”:null,“ datetime_6”:null,“ datetime_7”:null,“ datetime_8”:null,“ datetime_9”:null,“ datetime_10”:null,“ datetime_11”:null,“ datetime_12”:null ,“ datetime_13”:null,“ datetime_14”:null,“ datetime_15”:null,“ datetime_16”:null,“ datetime_17”:null,“ datetime_18”:null,“ datetime_19”:null,“ datetime_20”:null,“ datetime_21“:null,” d atetime_22“:null,” datetime_23“:null,” datetime_24“:null,” datetime_25“:null,” datetime_26“:null,” string_0“:” https://www.mistay.in/hotels-in-mumbai/?checkin_date=2018-10-09&checkin_slot=1&slot_count=1&guest_count=2&room_count=1“,” string_1“:” www.mistay。 in“,” string_2“:null,” string_3“:” Hotel Sai Palace Inn“,” string_4“:” 1“,” string_5“:” 1“,” string_6“:” 2“,” string_7“:” 1 “,” string_8“:” 1970“,” string_9“:null,” string_10“:” Syscon Infoway Pvt.Ltd。“,” string_11“:”-“,” string_12“:null,” string_13“:null,” string_14“:null,” string_15“:null,” string_16“:null,” string_17“:null,” string_18“:null,” string_19“:null,” string_20“:null,” string_21“:null,” string_22“ :null,“ string_23”:null,“ string_24”:null,“ string_25”:null,“ string_26”:null,“ text_0”:null,“ text_1”:null,“ text_2”:null,“ text_3”:null ,“ text_4”:null,“ text_5”:null,“ text_6”:null,“ text_7”:null,“ text_8”:null,“ text_9”:null,“ text_10”:null,“ text_11”:null,“ text_12“:null,” text_13“:null,” text_14“:null,” text_15“:null,” text_16“:null,” text_17“:null,” text_18“:null,” text_19“:null,” text_20“ :null,“ text_21”:null,“ text_22”:null,“ text_23”:null,“ text_24”:null,“ text_25”:null,“ text_26”:null,“ boolean_0”:n ull,“ boolean_1”:null,“ boolean_2”:null,“ boolean_3”:null,“ boolean_4”:null,“ boolean_5”:null,“ boolean_6”:null,“ boolean_7”:null,“ boolean_8”:null, “ boolean_9”:null,“ boolean_10”:null,“ boolean_11”:null,“ boolean_12”:null,“ boolean_13”:null,“ boolean_14”:null,“ boolean_15”:null,“ boolean_16”:null,“ boolean_17 “:null,” boolean_18“:null,” boolean_19“:null,” boolean_20“:null,” boolean_21“:null,” boolean_22“:null,” boolean_23“:null,” boolean_24“:null,” boolean_25“: null,“ boolean_26”:null,“ array_0”:[],“ array_1”:[],“ array_2”:[],“ array_3”:[],“ array_4”:[],“ array_5”:[], “ array_6”:[],“ array_7”:[],“ array_8”:[],“ array_9”:[],“ array_10”:[],“ array_11”:[],“ array_12”:[],“ array_13“:[],” array_14“:[],” array_15“:[],” array_16“:[],” array_17“:[],” array_18“:[],” array_19“:[],” array_20 “:[],” array_21“:[],” array_22“:[],” array_23“:[],” array_24“:[],” array_25“:[],” array_26“:[]}} >
import json
f2 = open('sample.json', 'w')
def file_write_from_head(fname, nlines):
from itertools import islice
with open(fname) as f:
for line in islice(f, nlines):
f2.write(line)
file_write_from_head('events.json', 1)
with open("sample.json", "r", encoding = "utf-8") as f:
data = json.load(f)
答案 0 :(得分:1)
我发现使您的代码正常工作的问题是您忘记了关闭读取文件。
因此,您需要执行以下操作:
f2.close()
之后
file_write_from_head('events.json', 1)
您可以了解有关close的更多信息。
您的完整代码段如下所示:
import json
f2 = open('sample.json', 'w')
def file_write_from_head(fname, nlines):
from itertools import islice
with open(fname) as f:
for line in islice(f, nlines):
f2.write(line)
file_write_from_head('events.json', 1)
f2.close()
with open("sample.json", "r", encoding = "utf-8") as f:
data = json.load(f)