python 插入年月日

ifmq2ha2  于 2023-08-02  发布在  Python
关注(0)|答案(2)|浏览(87)
BK_LIST =
["05 Sep 2020 OPENING BALANCE $30,152.02CR",
 "05 Sep Transfer to xxXXXX XXXXXXXXX app 100.00 $ $30,052.02CR",
 "27 Oct Transfer to xxXXXX XXXXXXXXX app 100.00 $ $31,800.06CR",
 "01 Nov Credit Interest $1.32 $31,601.38CR",
 "02 Nov Transfer to xxXXXX XXXXXXXXX app 51.00 $ $31,550.38CR",
 "30 Dec Transfer to xxXXXX XXXXXXXXX app 100.00 $ $27,800.00CR",
 "31 Dec Transfer to xxXXXX XXXXXXXXX app 100.00 $ $29,000.00CR",
 "01 Jan Credit Interest $1.27 $29,001.27CR",
 "26 Feb Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,900.00CR",
 "03 Mar Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,700.08CR",
 "04 Mar 2021 CLOSING BALANCE $28,700.08 CR"
 ]

字符串
我想在日期月份(05 Sep)后插入年份(2020)。预期输出:

27 Oct 2020, 01 Jan 2021 ...


1.我生成了一个缩写列表。2020年和2021年的月份和年份。

mth_yr_list =['Jan 2020', 'Feb 2020', 'Mar 2020', 'Apr 2020', 'May 2020', 'Jun 2020', 'Jul 2020', 'Aug 2020', 'Sep 2020', 'Oct 2020', 'Nov 2020', 'Dec 2020', 'Jan 2021', 'Feb 2021', 'Mar 2021', 'Apr 2021', 'May 2021', 'Jun 2021', 'Jul 2021', 'Aug 2021', 'Sep 2021', 'Oct 2021', 'Nov 2021', 'Dec 2021']


1.我从BK_LIST创建了一个月的列表。

list_of_mths =["Sep","Oct","Nov","Dec","Jan","Feb","Mar"]
My tried program:
jac =[]
for mth in list_of_mths:
    for mth1 in mth_yr_list:
        mth2 = mth1[:3].strip()    
        if mth ==mth2:
            jac.append(mth1)
        mth_yr_list.remove(mth1)
print(jac)


输出量:

['Sep 2020', 'Sep 2021', 'Oct 2020', 'Oct 2021', 'Dec 2021']


预期输出:

['Sep 2020', 'Oct 2020', 'Nov 2020','Dec 2020','Jan 2021','Feb 2021', 'Mar 2021']

eimct9ow

eimct9ow1#

您可以尝试:

import re

BK_LIST = [
    "05 Sep 2020 OPENING BALANCE $30,152.02CR",
    "05 Sep Transfer to xxXXXX XXXXXXXXX app 100.00 $ $30,052.02CR",
    "27 Oct Transfer to xxXXXX XXXXXXXXX app 100.00 $ $31,800.06CR",
    "01 Nov Credit Interest $1.32 $31,601.38CR",
    "02 Nov Transfer to xxXXXX XXXXXXXXX app 51.00 $ $31,550.38CR",
    "30 Dec Transfer to xxXXXX XXXXXXXXX app 100.00 $ $27,800.00CR",
    "31 Dec Transfer to xxXXXX XXXXXXXXX app 100.00 $ $29,000.00CR",
    "01 Jan Credit Interest $1.27 $29,001.27CR",
    "26 Feb Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,900.00CR",
    "03 Mar Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,700.08CR",
    "04 Mar 2021 CLOSING BALANCE $28,700.08 CR",
]

starting_month_idx = None
starting_date = None

months = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
]

out = []
for line in BK_LIST:
    if not starting_date and (m := re.search(r"^\d{2} (\w{3}) (\d{4})", line)):
        starting_month_idx = months.index(m.group(1))
        starting_date = int(m.group(2))
        out.append(line)
    elif starting_date and (m := re.search(r"^\d{2} (\w{3})(?! \d{4})", line)):
        idx = months.index(m.group(1))
        if idx < starting_month_idx:
            starting_month_idx = idx
            starting_date += 1
        out.append(re.sub(r"^\d{2} \w{3}", rf"\g<0> {starting_date}", line))
    else:
        out.append(line)

print(out)

字符串
印刷品:

[
    "05 Sep 2020 OPENING BALANCE $30,152.02CR",
    "05 Sep 2020 Transfer to xxXXXX XXXXXXXXX app 100.00 $ $30,052.02CR",
    "27 Oct 2020 Transfer to xxXXXX XXXXXXXXX app 100.00 $ $31,800.06CR",
    "01 Nov 2020 Credit Interest $1.32 $31,601.38CR",
    "02 Nov 2020 Transfer to xxXXXX XXXXXXXXX app 51.00 $ $31,550.38CR",
    "30 Dec 2020 Transfer to xxXXXX XXXXXXXXX app 100.00 $ $27,800.00CR",
    "31 Dec 2020 Transfer to xxXXXX XXXXXXXXX app 100.00 $ $29,000.00CR",
    "01 Jan 2021 Credit Interest $1.27 $29,001.27CR",
    "26 Feb 2021 Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,900.00CR",
    "03 Mar 2021 Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,700.08CR",
    "04 Mar 2021 CLOSING BALANCE $28,700.08 CR",
]

31moq8wy

31moq8wy2#

使用Pandas

import pandas as pd

BK_LIST = ["05 Sep 2020 OPENING BALANCE $30,152.02CR",
           "05 Sep Transfer to xxXXXX XXXXXXXXX app 100.00 $ $30,052.02CR",
           "27 Oct Transfer to xxXXXX XXXXXXXXX app 100.00 $ $31,800.06CR",
           "01 Nov Credit Interest $1.32 $31,601.38CR",
           "02 Nov Transfer to xxXXXX XXXXXXXXX app 51.00 $ $31,550.38CR",
           "30 Dec Transfer to xxXXXX XXXXXXXXX app 100.00 $ $27,800.00CR",
           "31 Dec Transfer to xxXXXX XXXXXXXXX app 100.00 $ $29,000.00CR",
           "01 Jan Credit Interest $1.27 $29,001.27CR",
           "26 Feb Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,900.00CR",
           "03 Mar Transfer to xxXXXX XXXXXXXXX app 100.00 $ $28,700.08CR",
           "04 Mar 2021 CLOSING BALANCE $28,700.08 CR"
           ]

BK_LIST = [x.replace('Transfer to', 'Transfer_to') for x in BK_LIST]
BK_LIST = [x.replace('Credit Interest', 'Credit_Interest None None None') for x in BK_LIST]
BK_LIST = [x.replace('OPENING BALANCE', 'OPENING_BALANCE') for x in BK_LIST]
BK_LIST = [x.split() for x in BK_LIST]

Map = {'Sep': '2020', 'Oct': '2020', 'Nov': '2020', 'Dec': '2020', 'Jan': '2021', 'Feb': '2021', 'Mar': '2021'}
df = pd.DataFrame(BK_LIST[1:-1],
                  columns=['Day', 'Month', 'Type', 'Account', 'ACC', 'Method', 'Movement', 's', 'Balance'])\
    .drop(['ACC', 's'], axis=1)
df['Year'] = [Map[x] for x in list(df.Month)]
df = df[['Day', 'Month', 'Year', 'Type', 'Account', 'Method', 'Movement', 'Balance']]

print(list((df.Month + ' ' + df.Year).unique()))

字符串
返回:

['Sep 2020', 'Oct 2020', 'Nov 2020', 'Dec 2020', 'Jan 2021', 'Feb 2021', 'Mar 2021']


您可以使用dataframe进一步操作:

print(df) 

  Day Month  Year             Type Account Method Movement       Balance
0  05   Sep  2020      Transfer_to  xxXXXX    app   100.00  $30,052.02CR
1  27   Oct  2020      Transfer_to  xxXXXX    app   100.00  $31,800.06CR
2  01   Nov  2020  Credit_Interest    None   None    $1.32          None
3  02   Nov  2020      Transfer_to  xxXXXX    app    51.00  $31,550.38CR
4  30   Dec  2020      Transfer_to  xxXXXX    app   100.00  $27,800.00CR
5  31   Dec  2020      Transfer_to  xxXXXX    app   100.00  $29,000.00CR
6  01   Jan  2021  Credit_Interest    None   None    $1.27          None
7  26   Feb  2021      Transfer_to  xxXXXX    app   100.00  $28,900.00CR
8  03   Mar  2021      Transfer_to  xxXXXX    app   100.00  $28,700.08CR

相关问题