Need to split the number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Need to split the number

    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
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Have you read the references?
    Code:
    >>> s='001001001001000001'
    >>> import textwrap
    >>> textwrap.wrap(s,3)
    ['001', '001', '001', '001', '000', '001']
    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.

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      Originally posted by ghostdog74
      Have you read the references?
      Code:
      >>> s='001001001001000001'
      >>> import textwrap
      >>> textwrap.wrap(s,3)
      ['001', '001', '001', '001', '000', '001']
      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

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by psbasha
        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.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by psbasha
          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:[code=Python]import re

          patt = re.compile(r'\d {1,3}')
          num = '00100100100100 000101'
          print patt.findall(nu m)

          # >>> ['001', '001', '001', '001', '000', '001'][/code]Remember pattnum?
          Taking it one more step:[code=Python]def fixed_field(s, n=3):
          return re.findall(r'\w {1,%s}' % n, s)

          print fixed_field('00 100100100100000 101')
          print fixed_field('00 100100100100000 10110007008abc' , 4)

          >>> ['001', '001', '001', '001', '000', '001', '01']
          ['0010', '0100', '1001', '0000', '0101', '1000', '7008', 'abc']
          >>> [/code]To assign list items to variables:[code=Python]
          for i, item in enumerate(fixed _field(num)):
          exec 'x%s = item' % (i+1)[/code]

          Comment

          Working...