【问题标题】:Python grab substring between two specific charactersPython 抓取两个特定字符之间的子字符串
【发布时间】:2021-12-11 10:46:28
【问题描述】:

我有一个包含数百个文件的文件夹,名称如下:

"2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"

约定: year_month_ID_zone_date_0_L2A_B01.tif"_0_L2A_B01.tif""zone" 永不改变)

我需要遍历每个文件并根据它们的名称构建路径以便下载它们。 例如:

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

路径约定需要是:path = year/month/ID_zone_date_0_L2A/B01.tif

我想制作一个循环,每次遇到"_" 字符时将我的字符串“切割”成几个部分,然后以正确的顺序缝合不同的部分以创建我的路径名。 我试过了,但没有用:

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

如何在 Python 上实现这一点?

【问题讨论】:

  • 那么,对于字符串中_ 的前两次和最后一次出现,基本上你想用/ 替换下划线_
  • 使用字符串拆分方法
  • 我很难理解这个例子,但我不确定名称和路径之间发生了什么变化,或者变化的模式是什么 - 例如,不确定 05 如何更改为 5 .
  • 就是这样,我没有决定约定。小于 10 的月份没有“0”,只有 1 个数字。不,我不想只用 / 替换 _,整个字符串都进行了重组。这就是我在回答中指定约定的原因:它从“year_month_ID_zone_date_0_L2A_B01.tif”变为“year/month/year_month_ID_zone_date_0_L2A/B08.tif”(所以不是从“_”替换为“/”,而是重新排列字符串)
  • * 路径约定错误:path = year/month/ID_zone_date_0_L2A/B01.tif

标签: python string loops substring


【解决方案1】:

不需要正则表达式——你可以使用split()

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

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

【讨论】:

    【解决方案2】:

    既然你只有一个分隔符,你也可以简单地使用 Python 内置的 split 函数:

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

    【讨论】:

      【解决方案3】:
      filename = "2017_05_S2B_7VEG_20170528_0_L2A_B01.tif"
      temp = filename.split('_')
      result = "/".join(temp)
      print(result)
      

      结果是 2017/05/S2B/7VEG/20170528/0/L2A/B01.tif

      【讨论】:

        【解决方案4】:

        试试下面的代码sn-p

        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
        

        【讨论】:

          【解决方案5】:

          也许你可以这样做:

          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
          

          【讨论】:

            猜你喜欢
            • 2015-06-24
            • 1970-01-01
            • 1970-01-01
            • 2013-03-16
            • 1970-01-01
            • 1970-01-01
            • 2013-09-12
            • 1970-01-01
            • 2013-01-31
            相关资源
            最近更新 更多