Home > OS >  Python grab substring between two specific characters
Python grab substring between two specific characters

Time:10-26

I have a folder with hundreds of files named like:

"2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"

Convention: year_month_ID_zone_date_0_L2A_B01.tif ("_0_L2A_B01.tif", and "zone" never change)

What I need is to iterate through every file and build a path based on their name in order to download them. For example:

name = "2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"
path = "2017/5/S2B_7VEG_20170528_0_L2A/B01.tif"

The path convention needs to be: path = year/month/year_month_ID_zone_date_0_L2A/B08.tif

I thought of making a loop which would "cut" my string into several parts every time it encounters a "_" character, then stitch the different parts in the right order to create my path name. I tried this but it didn't work:

import re

filename = 
"2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"

try:
    found = re.search('_(. ?)_', filename).group(1)
except AttributeError:
    # _ not found in the original string
    found = '' # apply your error handling

How could I achieve that on Python ?

CodePudding user response:

Since you only have one separator character, you may as well simply use Python's built in split function:

import os

items = filename.split('_')
year, month = items[:2]
new_filename = '_'.join(items[2:])

path = os.path.join(year, month, new_filename)

CodePudding user response:

No need for a regex -- you can just use split().

filename = "2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"
parts = filename.split("_")

year = parts[0]
month = parts[1]

CodePudding user response:

Try the following code snippet

filename = "2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"
found = re.sub('(\d )_(\d )_(.*)_(.*)\.tif', r'\1/\2/\3/\4.tif', filename)
print(found) # prints 2017/05/S2B_7VEG_20170528_0_L2A/B01.tif

CodePudding user response:

filename = "2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"
temp = filename.split('_')
result = "/".join(temp)
print(result)

result is 2017/05/S2B/7VEG/20170528/0/L2A/B01.tif

CodePudding user response:

Maybe you can do like this:

from os import listdir, mkdir from os.path import isfile, join, isdir

my_path = 'your_soure_dir'

files_name = [f for f in listdir(my_path) if isfile(join(my_path, f))]

def create_dir(files_name): for file in files_name: month = file.split('', '1')[0] week = file.split('', '2')[1] if not isdir(my_path): mkdir(month) mkdir(week) ### your download code

  • Related