如何在文件名中插入零以使它们具有相同的长度

时间:2019-07-18 21:45:33

标签: python file rename

我要使一大批JPG文件都具有相同长度的文件名。然后将它们转换为PDF。例如65-1A,66-10B,72-108C→65-001A,66-010B,72-108C。 XX-XXXX是目标长度,如果文件名较短,则在破折号后插入正确的0,以达到目标长度。

我尝试解析文件名,但是我想首先使文件名的长度相同

def parseFilename(file):
    baseFileName = os.path.splitext(file)[0]
    parts = baseFileName.split('-')
    year = parts[0]
    sequence = 0
    permitNumber = 0
    pageNumber = 0

    if len(parts) > 1:

        if len(parts[1]) == 2:
            permitNumber = (parts[1])[0:1]
            pageNumber = (parts[1])[1:2]

        if len(parts[1]) == 3:
            permitNumber = (parts[1])[0:2]
            pageNumber = (parts[1])[2:3]

        if len(parts[1]) == 4:
            permitNumber = (parts[1])[0:2]
            pageNumber = (parts[1])[2:3]
            sequence = (parts[1])[3:4]

    permitNumber = '{0}_{1}'.format(year, permitNumber)
    return (permitNumber, pageNumber)

由于文件名都是不同的长度,因此无法正确解析它们。我相信使它们具有相同的命名格式可能会更容易。

2 个答案:

答案 0 :(得分:2)

str.format()的填充可以在这里提供帮助。例如,

>>> '{:0>4}'.format('1A')
'001A'

:0>4格式为:

  • ::表示这是格式说明符
  • 0:以零填充
  • >:右对齐
  • 4:最多填充四个字符

我们可以更改其中的几个以使用不同的字符和不同的宽度填充:

>>> '{:#>6}'.format('1A')
'####1A'

这是一个执行填充的示例函数。作为奖励,它可以处理缺少的字段或带有连字符的字段。

def PadName(orig):
  # Split into fields.
  parts = orig.split('-')
  # Make sure there are enough fields.
  while len(parts) < 2:
    # This added field will become all zeros.
    parts.append('')
  # Pad the first two fields appropriately; leave the rest alone.
  parts[0] = '{:0>2}'.format(parts[0])
  parts[1] = '{:0>4}'.format(parts[1])
  # Join the fields back together.
  return '-'.join(parts)


testNames = (
  '72-108C',  # Already formatted
  '5-1A74',  # First part short
  '65-1A',  # Second part short
  '2-10C',  # Both parts short
  '5A1-1A74',  # First part long
  '5A-1A74C',  # Second part long
  '15-4-AA',  # Extra field
  'B',  # Only one field
  '',  # Empty
)


for baseFileName in testNames:
  padded = PadName(baseFileName)
  print('{:10}  >  {:10}'.format(baseFileName, padded))

输出:

72-108C     >  72-108C   
5-1A74      >  05-1A74   
65-1A       >  65-001A   
2-10C       >  02-010C   
5A1-1A74    >  5A1-1A74  
5A-1A74C    >  5A-1A74C  
15-4-AA     >  15-0004-AA
B           >  0B-0000   
            >  00-0000  

答案 1 :(得分:1)

将名称分隔为连字符。根据需要填充第二部分。重新加入零件。您所缺少的步骤是计算需要多少个前导0:这是文件名部分的长度四分钟。

tags = ["65-1A", "66-10B", "72-108C"]

for f in tags:
    parts = f.split('-')
    new_part1 = '0' * (4-len(parts[1])) + parts[1]
    new_part = '-'.join([parts[0], new_part1])
    print(new_part)

输出:

65-001A
66-010B
72-108C