473,585 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to split the number

440 Contributor
Hi,

I have a number ,i have to split it into 3 digits

For Example num = 001001001001000 001

After the split it should be

001
001
001
001
000
001
Is there any better way to split it

num = '00100100100100 0001'
x1 = num[0:3]
x2 = num[3:6]
x3 = num[6:9]
x4 = num[9:12]
x5 = num[12:15]
x6 = num[15:18]

Thanks
PSB
Jan 6 '08 #1
4 4672
ghostdog74
511 Recognized Expert Contributor
Have you read the references?
Expand|Select|Wrap|Line Numbers
  1. >>> s='001001001001000001'
  2. >>> import textwrap
  3. >>> textwrap.wrap(s,3)
  4. ['001', '001', '001', '001', '000', '001']                     
  5.  
You should really spend time reading up on Python docs because after 400+ posts, you should really be able to grasp the essence of Python.
Jan 6 '08 #2
psbasha
440 Contributor
Have you read the references?
Expand|Select|Wrap|Line Numbers
  1. >>> s='001001001001000001'
  2. >>> import textwrap
  3. >>> textwrap.wrap(s,3)
  4. ['001', '001', '001', '001', '000', '001']                     
  5.  
You should really spend time reading up on Python docs because after 400+ posts, you should really be able to grasp the essence of Python.
Thanks for the solution.

I have read couple of books on Python
- Dive in python
- Core Python Programming

and gone thru the links in the Internet.Python is having so many concepts,that we cannot get everything within short duration.So as we keep programming ,then thier will be more chance of learning more concepts( based on our requirements) than reading.I really appreciapte for your suggestion.

Could you please suggest some of the books you have read,which really help me in understanding the concepts in still better manner.

Thanks
PSB
Jan 6 '08 #3
ghostdog74
511 Recognized Expert Contributor
Could you please suggest some of the books you have read,which really help me in understanding the concepts in still better manner.
I have already told you one of my replies to your earlier posts, the official Python documentation site is where you should go. Read the whole web site as if its a book.
Jan 6 '08 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Hi,

I have a number ,i have to split it into 3 digits

For Example num = 001001001001000 001

After the split it should be

001
001
001
001
000
001
Is there any better way to split it

num = '00100100100100 0001'
x1 = num[0:3]
x2 = num[3:6]
x3 = num[6:9]
x4 = num[9:12]
x5 = num[12:15]
x6 = num[15:18]

Thanks
PSB
This is exactly what we did with re in another thread of yours:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. patt = re.compile(r'\d{1,3}')
  4. num = '00100100100100000101'
  5. print patt.findall(num)
  6.  
  7. # >>> ['001', '001', '001', '001', '000', '001']
Remember pattnum?
Taking it one more step:
Expand|Select|Wrap|Line Numbers
  1. def fixed_field(s, n=3):
  2.     return re.findall(r'\w{1,%s}' % n, s)
  3.  
  4. print fixed_field('00100100100100000101')
  5. print fixed_field('0010010010010000010110007008abc', 4)
  6.  
  7. >>> ['001', '001', '001', '001', '000', '001', '01']
  8. ['0010', '0100', '1001', '0000', '0101', '1000', '7008', 'abc']
  9. >>> 
To assign list items to variables:
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(fixed_field(num)):
  2.     exec 'x%s = item' % (i+1)
Jan 6 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4890
by: nieuws | last post by:
Hi, I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this : I have a txt file with the following data : 1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael...
9
1762
by: martin | last post by:
Hi, a very newbie question. How do I split the adress and number to 2 variables? ex. "Kingsroad 347" = variabel1 = "Kingsroad" variabel2 = "347" Ill guess i have to search the string from left to right after first apperence of a number, but how do i do that? Regards Martin
3
9653
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3... field1...field2...field3...
12
1307
by: John Frame | last post by:
Hi, I've got a Python program that I'm trying to edit, and I need some help. If I would like to read a matrix from a previously created text file into a two dimensional array, how would I do that? Like, if in the txt file, I had the following matrix formatted numbers with 5 rows and 10 columns, and each number is separated by a single...
8
1055
by: Jack | last post by:
Hello, I need to split: 2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1 3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1 4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1 5 2484 1.00 195.20 2483.7 -36.5 1.1 -30.5 0.1 6 2962 0.90 200.90 2961.6 -44.0 -1.3 -35.3 0.0
2
4757
by: Transcend2030 | last post by:
Hi, I'm having problems with string.split() My problem is; I have a sentence which the user inputs, I then input a word and the number of times that word appears in the sentence is displayed. Is there a regex expression which will exclude every word except "word" which I entered. So that words.length will actually display the number of...
7
9201
thatos
by: thatos | last post by:
Here is the EdgeList class class Graph { protected int numvertices; protected int numedges; protected boolean directed; protected EdgeList adjlist ; // Constructors
3
1819
by: Eric_Dexter | last post by:
I am trying to take some data in file that looks like this command colnum_1 columnum_2 and look for the command and then cange the value in the collum(word) number indicated. I am under the impression I need enumerate but I am not sure what to do with it any help would be nice. import sys
2
1655
by: ogo796 | last post by:
Hi guys am having a problem with a split(),i retrieve line from the text file and i wanna split that line.i manage to split two words but splitting the string enclosed on brackets it seems to be a problem to me. can someone look at my code and please help me maybe i am missing something. <?php $q=$_POST; $jaar=$_POST;...
0
8195
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5707
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5386
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3856
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2340
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1444
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.