как использовать os.scandir для зацикливания всех растровых файлов в одной папке

Я предпочитаю использовать простые строковые функции, такие как INSTR, SUBSTR и REPLACE, но в этом случае он гораздо читабельнее с помощью простого REGEXP-REPLACE:

with testtab as(
  select '[...]#Product offer ID=505618817#Promotion effective date=20180403#Promotion expiration date=20180703#Promotion indicator=Y
#[...]#Waive ind= #' as text from dual)
select regexp_replace(text, 'Promotion expiration date=\d*', 'Promotion expiration date=' || to_char(sysdate, 'YYYYMMDD') ) from testtab;

\ d * - от 0 до бесконечности таких как '', 345345 или 4356456345634563546. Он совпадает с #. Это означает, что он заменяет Promotion expiration date=20180606, а также Promotion expiration date=345665756567

-2
задан loula melyacou 19 January 2019 в 21:54
поделиться

1 ответ

Я не тестировал этот код, но, возможно, он может помочь вам или дать представление о том, как решить проблему.

raster_path = "D:/MODIS/"
# ...
with os.scandir(raster_path) as p:
    for raster in p:
        if not raster.name.startswith('.') and entry.is_file():
            raster = gdal.Open(raster.name)
            bands = raster.RasterCount
            # ...

Это должно выглядеть примерно так.

row_list2000 = [] # I'm guessing that should be out of the loop.
raster_path = "D:/MODIS/" # That's the folder you want to search in for raster files.

with os.scandir(raster_path) as p: # Open the folder containing the rasters as p.
    for raster in p: # Get each raster in the folder.
        if not raster.name.endswith(".tif"): # Only show the files whose name ends with dot tif.
            raster_file = os.path.join(raster_path, raster.name)) # Get the full path of the raster, you'll need it in order to open the raster file and access its data. 
            raster = gdal.Open(raster_file) # Open the current raster file.

            # The rest is your code...

            bands = raster.RasterCount

            for band in range(1, bands+1):
                data = raster.GetRasterBand(band).ReadAsArray().astype('float')
                mean = np.mean(data[data != 0]) #calculate mean without value 0
                row_list2000.append({
                    'raster': os.path.basename(raster_name),
                    'band': band,
                    'mean': mean,
                    'year': 2000,
                    'date': 325,
                })
                print("Band %s: Mean = %s" % (band, round(mean, 2)))

Если вы используете версию Python ниже 3.6 (как я). Вам нужно заменить

with os.scandir(path) as p:
    for f in p:
        print(f.name)

на

for p in os.scandir(path):
    print(f.name)

, иначе вы получите ошибку AttributeError: __exit__.

0
ответ дан Remy J 19 January 2019 в 21:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: