python中字符串的算术[减号]

python中字符串的算术[减号],python,Python,我有一个字符串,它基本上是一个CSV文件的头,我必须从中提取月份,然后通过在它前面附加一个“0”将其转换为字符串,以与其他值进行比较 标题- HGLOABCD8PSGL_ZXFH J20190603NXT_APAC 由此,我需要从20190603(06)中提取月份,然后创建一个类似于['006','005']的列表,该列表的第二个元素将是标题中给定月份的前一个月 此外,标题也可能类似于月份不同的地方 HGLOABCD8PSGL_ZXFH J20191003NXT_APAC 我已经为第一个元素

我有一个字符串,它基本上是一个CSV文件的头,我必须从中提取月份,然后通过在它前面附加一个“0”将其转换为字符串,以与其他值进行比较

标题-

HGLOABCD8PSGL_ZXFH J20190603NXT_APAC
由此,我需要从20190603(06)中提取月份,然后创建一个类似于['006','005']的列表,该列表的第二个元素将是标题中给定月份的前一个月

此外,标题也可能类似于月份不同的地方

HGLOABCD8PSGL_ZXFH J20191003NXT_APAC
我已经为第一个元素编写了类似的内容,但不确定如何减去一个月,然后在其中附加“0”

acc_period = []
acc_period.append('0'+str(header)[26:28])

acc_period.append(int('0') + int(str(header)[26:28])-1)
print (acc_period)
试试正则表达式:

import re

output = list()
header = 'HGLOABCD8PSGL_ZXFH J20190103NXT_APAC'
#Using the regex pattern '\d*' this will fnid all the numeric sequences in the input string
find_all_numbers = re.findall('\d*', header)

#Filtering out any empty string resulted from extraction
numbers = [num for num in find_all_numbers if len(num)==8]

#Getting the largest number which is most of the time going to be the date in your case
date = numbers[0]

#Unpacking the data using string slicing

year, month, day = date[:4], date[4:6], date[6:]

#Using string format defining the desired format using left 0 padding
current_month, previous_month = '{0:03d}'.format(int(month)), '{0:03d}'.format(int(month)-1)
if previous_month =='000':
    previous_month = '012'
output.extend((current_month, previous_month))
print(output)
试试正则表达式:

import re

output = list()
header = 'HGLOABCD8PSGL_ZXFH J20190103NXT_APAC'
#Using the regex pattern '\d*' this will fnid all the numeric sequences in the input string
find_all_numbers = re.findall('\d*', header)

#Filtering out any empty string resulted from extraction
numbers = [num for num in find_all_numbers if len(num)==8]

#Getting the largest number which is most of the time going to be the date in your case
date = numbers[0]

#Unpacking the data using string slicing

year, month, day = date[:4], date[4:6], date[6:]

#Using string format defining the desired format using left 0 padding
current_month, previous_month = '{0:03d}'.format(int(month)), '{0:03d}'.format(int(month)-1)
if previous_month =='000':
    previous_month = '012'
output.extend((current_month, previous_month))
print(output)
使用正则表达式

例:

输出:

使用正则表达式

例:

输出:


您需要将月份存储在整数变量中并减去1,直到达到0条件并继续将结果添加到列表中。您需要将月份存储在整数变量中并减去1,直到达到0条件,并继续将结果添加到列表中。感谢您的回复。我只是想知道我应该如何阅读这段代码,我是python新手,我想学习这些类型的转换以备将来使用:欢迎使用mradul。你的意思是你想对脚本行进行解释吗?是的,如果可能的话,我也得到了一个错误类型错误:预期的字符串或字节类对象得到了错误需要将变量转换为STRIt当我更改日期以反映月份为01时,代码中似乎有一个错误,然后它创建的第二个元素为“000”,这是应该是'012'还有一件事就是得到最大的数字,这在大多数情况下都是日期,在你的情况下,我需要从标题中的第一个日期字符串得到月份,而不是从最大的数字,所以我们不能在这里做这个假设谢谢你的回复,只是想知道我应该如何阅读这段代码,我对python和想学习这些类型的转换供我将来使用:不客气,mradul。你的意思是你想对脚本行进行解释吗?是的,如果可能的话,我也得到了一个错误类型错误:预期的字符串或字节类对象得到了错误需要将变量转换为STRIt当我更改日期以反映月份为01时,代码中似乎有一个错误,然后它创建的第二个元素为“000”,这是应该是“012”只是另外一件事得到最大的数字,在大多数情况下,这将是您的情况下的日期我需要从标题中的第一个日期字符串得到月份,而不是从最大的数字,因此我们不能在这里作出此假设感谢您编写的答案,它也解决了我的问题,我将看看哪个更易于使用和管理以备将来使用,也是我更了解的一个:感谢您写下答案,它也解决了我的问题。我将看看哪一个更易于使用和管理以备将来使用,也是我更了解的一个:
006 005
010 009
001 012