0

I'm new in Python and I struggle with a probably an easy problem for all of you out there and maybe you could help me please

Basically I would need a function that reads a continuous string and break it as following: first 5 characters, inserts comma, next 6 characters, inserts comma, next 6 characters, inserts comma, inserts new line and then repeats

The problem: my string is:

"CARMD000000000003FEFFE000004000004BCCXT000009000025BBT01000035000025"

I need to divide this string into comma by following rule: 5-6-6 \n

Expected result:

CARMD,000000,000003, 

FEFFE,000004,000004, 

BCCXT,000009,000025,

BBT01,000035,000025,

Thank you for your help.

4 Answers 4

1
import re

text = "CARMD000000000003FEFFE000004000004BCCXT000009000025BBT01000035000025"
match = re.findall(r'([A-Z]{5})(\d{6})(\d{6})', text)
lines = [','.join(item) for item in match]
print(*lines, sep='\n')

out:

CARMD,000000,000003
FEFFE,000004,000004
BCCXT,000009,000025

use regex to match text, will return a list of tuple:

[('CARMD', '000000', '000003'), ('FEFFE', '000004', '000004'), ('BCCXT', '000009', '000025')]

than use list comprehension to construct a list, each element in list is string, concatenated by the tuple using ','.

lines:

['CARMD,000000,000003', 'FEFFE,000004,000004', 'BCCXT,000009,000025']
Sign up to request clarification or add additional context in comments.

2 Comments

This Solution doesn't save the final string into a variable. It just prints it out. I guess that's good if you only want it for one-time use.
@Cheyn Shmuel i store data in list 'lines'
1

"One-line" solution using re.findall() and str.join() functions:

s = "CARMD000000000003FEFFE000004000004BCCXT000009000025BBT01000035000025"
chunks = ',\n'.join(','.join(t) for t in re.findall(r'(\w{5})(\w{6})(\w{6})', s))

print(chunks)

The output:

CARMD,000000,000003,
FEFFE,000004,000004,
BCCXT,000009,000025,
BBT01,000035,000025

Comments

1

An alternative to using regex is using list slicing with a for loop like below:

>>> s = 'CARMD000000000003FEFFE000004000004BCCXT000009000025BBT01000035000025'
>>> 
>>> for i in range(len(s) / 17):
...     temp = s[i*17:i*17+17]
...     print '{}, {}, {},'.format(temp[:5], temp[5:11], temp[11:17])
...
CARMD, 000000, 000003,
FEFFE, 000004, 000004,
BCCXT, 000009, 000025,
BBT01, 000035, 000025,

Comments

0

A simple program like this should do the trick:

s = "CARMD000000000003FEFFE000004000004BCCXT000009000025BBT01000035000025"
new_s = ''
while s:
    for x in (5, 6, 6):
        new_s += s[:x]
        s = s[x:]
        new_s += ','
    new_s += '\n'

print(new_s)

output:

CARMD,000000,000003,
FEFFE,000004,000004,
BCCXT,000009,000025,
BBT01,000035,000025,

I found a nested loop efficient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.