Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pattern Split String
#6
from collections import namedtuple
def parse(line):
    Field = namedtuple('Field', field_names=('name', 'start', 'length'))
    mapping = {'000A':(Field('REC_CNTR', 0, 5), Field('REC_TYPE', 5, 4),
                       Field('DATETIME', 9, 14), Field('NAME', 23, 14),
                       Field('AMOUNT', 37, 8), Field('ADDRESS_LINE_1', 45, 75)),
               '000B':(Field('REC_CNTR', 0, 5), Field('REC_TYPE', 5, 4),
                       Field('BILL_NO', 9, 14), Field('BILL_TAX', 23, 8),
                       Field('BILL_DUE_DATE', 31, 14))}
    rec_type = line[5:9]
    # rec_field_names = (fld.name for fld in mapping[rec_type])
    # Record = namedtuple("Record", field_names=rec_field_names)
    # return Record(**{fld.name:line[fld.start:fld.start+fld.length] for fld in mapping[rec_type]})
    return {fld.name:line[fld.start:fld.start+fld.length] for fld in mapping[rec_type]}


# Sample Test Data
sample_lines = ["00001000A20181022342320Mr.ABC DEF VAL  234.34ADDRESS LINE1",
                "00002000BISSNO-CF123    0023.3420180722",
                "00003000BISSNO-CF124   12327.3420180810"]
 
data = [parse(line) for line in sample_lines]
for rec in data:
    print(rec)
this will output
Output:
{'AMOUNT': ' 234.34', 'REC_CNTR': '00001', 'DATETIME': '20181022342320', 'ADDRESS_LINE_1': 'ADDRESS LINE1', 'NAME': 'Mr.ABC DEF VAL', 'REC_TYPE': '000A'} {'BILL_NO': 'ISSNO-CF123 ', 'BILL_DUE_DATE': '20180722', 'BILL_TAX': ' 0023.34', 'REC_CNTR': '00002', 'REC_TYPE': '000B'} {'BILL_NO': 'ISSNO-CF124 ', 'BILL_DUE_DATE': '20180810', 'BILL_TAX': '12327.34', 'REC_CNTR': '00003', 'REC_TYPE': '000B'}
if you uncomment lines #11-#13 and comment out line#14 you will go one step further and use namedtuple also for record, not only fields
Output:
Record(REC_CNTR='00001', REC_TYPE='000A', DATETIME='20181022342320', NAME='Mr.ABC DEF VAL', AMOUNT=' 234.34', ADDRESS_LINE_1='ADDRESS LINE1') Record(REC_CNTR='00002', REC_TYPE='000B', BILL_NO='ISSNO-CF123 ', BILL_TAX=' 0023.34', BILL_DUE_DATE='20180722') Record(REC_CNTR='00003', REC_TYPE='000B', BILL_NO='ISSNO-CF124 ', BILL_TAX='12327.34', BILL_DUE_DATE='20180810')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Pattern Split String - by sripylearn - Aug-10-2018, 04:03 PM
RE: Pattern Split String - by buran - Aug-10-2018, 04:10 PM
RE: Pattern Split String - by sripylearn - Aug-10-2018, 05:19 PM
RE: Pattern Split String - by buran - Aug-10-2018, 07:01 PM
RE: Pattern Split String - by buran - Aug-10-2018, 07:38 PM
RE: Pattern Split String - by buran - Aug-10-2018, 08:07 PM
RE: Pattern Split String - by sripylearn - Aug-12-2018, 04:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,758 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,269 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  [split] Parse Nested JSON String in Python mmm07 4 1,625 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,362 Jul-11-2022, 08:52 AM
Last Post: japo85
  Split string knob 2 1,926 Nov-19-2021, 10:27 AM
Last Post: ghoul
  Split string between two different delimiters, with exceptions DreamingInsanity 2 2,088 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  split string enigma619 1 2,107 May-20-2020, 02:47 PM
Last Post: perfringo
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 6,715 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,867 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  input string split Eric7Giants 3 3,084 Nov-13-2019, 07:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020