1

您好我想将文件(在本例中为 Landsat 7 元数据文件)的内容转换为使用 Python 2.7 的文件内容定义的一系列变量。文件内容如下所示:

  GROUP = L1_METADATA_FILE
      GROUP = METADATA_FILE_INFO
        ORIGIN = "Image courtesy of the U.S. Geological Survey"
        REQUEST_ID = "0101305309253_00043"
        LANDSAT_SCENE_ID = "LE71460402010069SGS00"
        FILE_DATE = 2013-06-02T11:19:59Z
        STATION_ID = "SGS"
        PROCESSING_SOFTWARE_VERSION = "LPGS_12.2.1"
        DATA_CATEGORY = "NOMINAL"
      END_GROUP = METADATA_FILE_INFO
      GROUP = PRODUCT_METADATA
        DATA_TYPE = "L1T"
        ELEVATION_SOURCE = "GLS2000"
        OUTPUT_FORMAT = "GEOTIFF"
        EPHEMERIS_TYPE = "DEFINITIVE"
        SPACECRAFT_ID = "LANDSAT_7"
        SENSOR_ID = "ETM"
        SENSOR_MODE = "BUMPER"
        WRS_PATH = 146
        WRS_ROW = 040
        DATE_ACQUIRED = 2010-03-10

GROUP = IMAGE_ATTRIBUTES
    CLOUD_COVER = 0.00
    IMAGE_QUALITY = 9
    SUN_AZIMUTH = 137.38394502
    SUN_ELEVATION = 48.01114126
    GROUND_CONTROL_POINTS_MODEL = 55
    GEOMETRIC_RMSE_MODEL = 3.790
    GEOMETRIC_RMSE_MODEL_Y = 2.776
    GEOMETRIC_RMSE_MODEL_X = 2.580
  END_GROUP = IMAGE_ATTRIBUTES


    Example of interested variable items:

    GROUP = MIN_MAX_RADIANCE
        RADIANCE_MAXIMUM_BAND_1 = 293.700
        RADIANCE_MINIMUM_BAND_1 = -6.200
        RADIANCE_MAXIMUM_BAND_2 = 300.900
        RADIANCE_MINIMUM_BAND_2 = -6.400
        RADIANCE_MAXIMUM_BAND_3 = 234.400
        RADIANCE_MINIMUM_BAND_3 = -5.000
        RADIANCE_MAXIMUM_BAND_4 = 241.100
        RADIANCE_MINIMUM_BAND_4 = -5.100
        RADIANCE_MAXIMUM_BAND_5 = 47.570
        RADIANCE_MINIMUM_BAND_5 = -1.000
        RADIANCE_MAXIMUM_BAND_6_VCID_1 = 17.040
        RADIANCE_MINIMUM_BAND_6_VCID_1 = 0.000
        RADIANCE_MAXIMUM_BAND_6_VCID_2 = 12.650
        RADIANCE_MINIMUM_BAND_6_VCID_2 = 3.200
        RADIANCE_MAXIMUM_BAND_7 = 16.540
        RADIANCE_MINIMUM_BAND_7 = -0.350
        RADIANCE_MAXIMUM_BAND_8 = 243.100
        RADIANCE_MINIMUM_BAND_8 = -4.700
      END_GROUP = MIN_MAX_RADIANCE

我对其他想法持开放态度,因为我不需要所有条目作为变量,只是一个选择。而且我看到一些标题不止一次列出。即 GROUP 被多次使用。我需要能够选择某些变量(整数值)并在其他代码区域的公式中使用。任何帮助将不胜感激(新手python编码器)。

4

1 回答 1

0

我不确定您到底在寻找什么,但可能是这样的:

s = '''GROUP = L1_METADATA_FILE
  GROUP = METADATA_FILE_INFO
    ORIGIN = "Image courtesy of the U.S. Geological Survey"
    REQUEST_ID = "0101305309253_00043"
    LANDSAT_SCENE_ID = "LE71460402010069SGS00"
    FILE_DATE = 2013-06-02T11:19:59Z
    STATION_ID = "SGS"
    PROCESSING_SOFTWARE_VERSION = "LPGS_12.2.1"
    DATA_CATEGORY = "NOMINAL"
  END_GROUP = METADATA_FILE_INFO
  GROUP = PRODUCT_METADATA
    DATA_TYPE = "L1T"
    ELEVATION_SOURCE = "GLS2000"
    OUTPUT_FORMAT = "GEOTIFF"
    EPHEMERIS_TYPE = "DEFINITIVE"
    SPACECRAFT_ID = "LANDSAT_7"
    SENSOR_ID = "ETM"
    SENSOR_MODE = "BUMPER"
    WRS_PATH = 146
    WRS_ROW = 040
    DATE_ACQUIRED = 2010-03-10'''


output = {} #Dict
for line in s.split("\n"): #Iterates through every line in the string
    l = line.split("=") #Seperate by "=" and put into a list
    output[l[0].strip()] = l[1].strip() #First word is key, second word is value

print output #Output is a dictonary containing all key-value pairs in your metadata seperated by "="

print output["SENSOR_ID"] #Outputs "ETM"

===============

编辑:

f = open('metadata.txt', 'r') #open file for reading

def build_data(f): #build dictionary

    output = {} #Dict
    for line in f.readlines(): #Iterates through every line in the string
        if "=" in line: #make sure line has data as wanted
            l = line.split("=") #Seperate by "=" and put into a list
            output[l[0].strip()] = l[1].strip() #First word is key, second word is value

    return output #Returns a dictionary with the key, value pairs.

data = build_data(f)

print data["IMAGE_QUALITY"] #prints 9
于 2013-06-19T05:15:40.593 回答