对于python来说是新手,所以如果这是一个愚蠢的问题,请原谅我,但我一直在尝试循环提取.json文件中的某些信息(特别是日期和一个值)以提取时间系列。由于我有300多个文件,因此我希望自动执行此操作,以便轻松创建某些值的时间序列。我已经设法打印了数据,但是未能将这些信息提取到一个文本文件中,而该文本文件可以在excel中读取。
请附上我尝试提取的示例.json文件和到目前为止的代码。谢谢!
{ “ AcquasitionInfo”:{ “日期”:{ “日”:27, “月”:3, “年份”:2011 }, “ EarthSunDistance”:0.9977766, “ SolarAzimuth”:154.94013617, “ SolarZenith”:53.1387049, “时间”: { “小时”:11 “分钟”:0, “第二”:21 }, “ sensorAzimuth”:0.0, “ sensorZenith”:0.0 }, “ FileInfo”:{ “ CLOUD_MASK”:“ LS5TM_20110327_lat53lon354_r23p204_clouds.kea”, “ FileBaseName”:“ LS5TM_20110327_lat53lon354_r23p204”, “ IMAGE_DEM”:“ LS5TM_20110327_lat53lon354_r23p204_dem.kea”, “ METADATA”:“ LS5TM_20110327_lat53lon354_r23p204_meta.json”, “ ProviderMetadata”:“ LT05_L1TP_204023_20110327_20161208_01_T1_MTL.txt”, “ RADIANCE”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_mclds_rad.kea”, “ RADIANCE_WHOLE”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_rad.kea”, “ SREF_6S_IMG”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_mclds_topshad_rad_srefdem.kea”, “ STD_SREF_IMG”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_mclds_topshad_rad_srefdem_stdsref.kea”, “ THERMAL_BRIGHT”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_thrad_thermbright.kea”, “ THERMAL_BRIGHT_WHOLE”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_thrad_thermbright.kea”, “ THERM_RADIANCE_WHOLE”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_thermrad.kea”, “ TOA”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_mclds_rad_toa.kea”, “ TOA_WHOLE”:“ LS5TM_20110327_lat53lon354_r23p204_vmsk_rad_toa.kea”, “ TOPO_SHADOW_MASK”:“ LS5TM_20110327_lat53lon354_r23p204_toposhad.kea”, “ VALID_MASK”:“ LS5TM_20110327_lat53lon354_r23p204_valid.kea”, “ VIEW_ANGLE”:“ LS5TM_20110327_lat53lon354_r23p204_viewangle.kea” }, “ ImageInfo”:{ “ CellSizeRefl”:30.0, “ CellSizeTherm”:30.0, “ CloudCover”:52.0, “ CloudCoverLand”:79.0 }, “ LocationInfo”:{ “地理”:{ “ BBOX”:{ “ BLLat”:52.06993, “ BLLon”:-5.34028, “ BRLat”:52.08621, “ BRLon”:-1.72003, “ TLLat”:54.09075, “ TLLon”:-5.45257, “ TRLat”:54.10827, “ TRLon”:-1.65856 }, “ CentreLat”:53.10330325240661, “ CentreLon”:-3.5429440927905724 }, “预计”:{ “ BBOX”:{ “ BLX”:354735.0, “ BLY”:5776815.0, “ BRX”:572985.0, “ BRY”:5776815.0, “ TLX”:354735.0, “ TLY”:5992035.0, “ TRX”:572985.0, “ TRY”:5992035.0 }, “ CentreX”:463860.0, “中心”:5884425.0, “ VPOLY”:{ “ MaxXX”:572985.0, “ MaxXY”:5950185.0, “ MaxYX”:405795.0, “ MaxYY”:5992035.0, “ MinXX”:354735.0, “ MinXY”:5819025.0, “ MinYX”:521775.0, “ MinYY”:5776815.0 } } }, “ ProductsInfo”:{ “ ARCSIProducts”:[ “ CLOUDS”, “ DOSAOTSGL”, “ STDSREF”, “元数据” ], “ ARCSI_AOT_RANGE_MAX”:0.5, “ ARCSI_AOT_RANGE_MIN”:0.05, “ ARCSI_AOT_VALUE”:0.5, “ ARCSI_CLOUD_COVER”:0.627807080745697, “ ARCSI_LUT_ELEVATION_MAX”:1100, “ ARCSI_LUT_ELEVATION_MIN”:-100, “ ProcessDate”:{ “日”:11 “月”:7 “年份”:2018 }, “ ProcessTime”:{ “小时”:7 “分钟”:24, “第二”:55 } }, “ SensorInfo”:{ “ ARCSISensorName”:“ LS5TM”, “路径”:204, “行”:23, “ SensorID”:“ TM”, “ SpacecraftID”:“ LANDSAT_5” }, “ SoftwareInfo”:{ “名称”:“ ARCSI”, “ URL”:“ http://www.rsgislib.org/arcsi”, “版本”:“ 3.1.4” }
import glob
import json
jsonfile = glob.glob('*.json')
with open(jsonfile[0]) as f:
data = json.load(f)
print(data["AcquasitionInfo"]["Date"]["Day"])
print(data["AcquasitionInfo"]["Date"]["Month"])
print(data["AcquasitionInfo"]["Date"]["Year"])
print(data["ProductsInfo"]["ARCSI_AOT_VALUE"])
with open('data.txt', 'w') as outfile:
json.dump(["ProductsInfo"]["ARCSI_AOT_VALUE"], outfile)
答案 0 :(得分:0)
您在最后一行忘记了data
:
import glob
import json
jsonfile = glob.glob('*.json')
with open(jsonfile[0]) as f:
data = json.load(f)
print(data["AcquasitionInfo"]["Date"]["Day"])
print(data["AcquasitionInfo"]["Date"]["Month"])
print(data["AcquasitionInfo"]["Date"]["Year"])
print(data["ProductsInfo"]["ARCSI_AOT_VALUE"])
with open('data.txt', 'w') as outfile:
json.dump(data["ProductsInfo"]["ARCSI_AOT_VALUE"], outfile)
编辑:
您可以这样:
import json
import os
for file in os.listdir("."):
if file.endswith(".json"):
with open(file) as f:
data = json.load(f)
with open('data.txt', 'a') as outfile:
json.dump(data["ProductsInfo"]["ARCSI_AOT_VALUE"], outfile)
outfile.write(';')