Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a list of numbers stored in one single column in Excel.
All numbers are 11 digit length.
For example 00002451018 I need to transform it in this specific pattern 00 00 2 451 018.
The pattern is 2 2 1 3 3.
How can I do it in Python?

What I have tried:

I didn't have any idea how to make it. I found a way how to split the digits into individual, but not how to make it this specific pattern.
Posted
Updated 8-Nov-21 5:20am

For a simple print statement you could do:
Python
number = '00002451018'
print(number[0:2], number[2:4], number[4:5], number[5:8], number[8:])


[edit]
To do this in a loop based on a list of field widths:
Python
# if number is not a text string 
number = F'{number:011d}'
f1 = 0
for val in [2, 2, 1, 3, 3]:
    f2 = f1 + val
    print(number[f1:f2], end=' ')
    f1 = f2
print('')


[/edit]
 
Share this answer
 
v2
Comments
vganchev 8-Nov-21 10:24am    
Hello and thanks for your proposal. I would try to do it in loop and change from the old pattern to the new.
Richard MacCutchan 8-Nov-21 10:45am    
See my updated solution above.
vganchev 8-Nov-21 10:52am    
One second to check it. The second comment was before your update.
Richard MacCutchan 8-Nov-21 10:52am    
Sorry I don't understand. Where is this coming from?
vganchev 8-Nov-21 10:58am    
After your update somehow it's not working. When I put manually the number in the variable number is working fine, but when I use the column from the priceListTest(excel file) is not working.
I create screenshot and upload it into imgur: https://imgur.com/IUMglX3

It's returning properly only row 8 and 10, but it's still in the old way.

Here is when I type it manually: https://imgur.com/LOPtho1
and your solution here it's working. Maybe it's because of the data is taken from excel column?
# if number is not a text string
number = F'{priceListTest['Парт номер']:011d}'
f1 = 0
for val in [2, 2, 1, 3, 3]:
f2 = f1 + val
print(number[f1:f2], end=' ')
f1 = f2
print('')
<pre>

This is also not working. The numbers are containing in this column priceListTest['Парт номер']
 
Share this answer
 
# if number is not a text string 
test = int(priceListTest['Column1'])
print(test)
number = F'{test:011d}'
f1 = 0
for val in [2, 2, 1, 3, 3]:
    f2 = f1 + val
    print(number[f1:f2], end=' ')
    f1 = f2
print('')


This was the working way. I will mark your proposal as solution. For some strange reason the cell is containing only numbers, but when we take it to variable it was transformed into string which cause this problem to stay in the old way pattern. Thank you Richard MacCutchan!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900