Split string by count of characters

前端 未结 8 2382
青春惊慌失措
青春惊慌失措 2020-11-30 07:45

I can\'t figure out how to do this with string methods:

In my file I have something like 1.012345e0070.123414e-004-0.1234567891.21423... which means there is no deli

相关标签:
8条回答
  • 2020-11-30 07:54
    line = "1.012345e0070.123414e-004-0.1234567891.21423"
    firstNumber = line[:12]
    restOfLine = line[12:]
    
    print firstNumber
    print restOfLine
    

    Output:

    1.012345e007
    0.123414e-004-0.1234567891.21423
    
    0 讨论(0)
  • 2020-11-30 07:56

    Try this function:

    x = "1.012345e0070.123414e-004-0.1234567891.21423"
    while len(x)>0:
      v = x[:12]
      print v
      x = x[12:]
    
    0 讨论(0)
  • 2020-11-30 08:00

    Since you want to iterate in an unusual way, a generator is a good way to abstract that:

    def chunks(s, n):
        """Produce `n`-character chunks from `s`."""
        for start in range(0, len(s), n):
            yield s[start:start+n]
    
    nums = "1.012345e0070.123414e-004-0.1234567891.21423"
    for chunk in chunks(nums, 12):
        print chunk
    

    produces:

    1.012345e007
    0.123414e-00
    4-0.12345678
    91.21423
    

    (which doesn't look right, but those are the 12-char chunks)

    0 讨论(0)
  • 2020-11-30 08:00

    You're looking for string slicing.

    >>> x = "1.012345e0070.123414e-004-0.1234567891.21423"
    >>> x[2:10]
    '012345e0'
    
    0 讨论(0)
  • 2020-11-30 08:00

    you can do it like this:

    step = 12
    for i in range(0, len(string), 12):
        slice = string[i:step]
        step += 12
    

    in this way on each iteration you will get one slice of 14 characters.

    0 讨论(0)
  • 2020-11-30 08:05

    I stumbled on this while looking for a solution for a similar problem - but in my case I wanted to split string into chunks of differing lengths. Eventually I solved it with RE

    In [13]: import re
    
    In [14]: random_val = '07eb8010e539e2621cb100e4f33a2ff9'
    
    In [15]: dashmap=(8, 4, 4, 4, 12)
    
    In [16]: re.findall(''.join('(\S{{{}}})'.format(l) for l in dashmap), random_val)
    Out[16]: [('07eb8010', 'e539', 'e262', '1cb1', '00e4f33a2ff9')]
    

    Bonus

    For those who may find it interesting - I tried to create pseudo-random ID by specific rules, so this code is actually part of the following function

    import re, time, random 
    def random_id_from_time_hash(dashmap=(8, 4, 4, 4, 12)):
         random_val = ''
         while len(random_val) < sum(dashmap):
             random_val += '{:016x}'.format(hash(time.time() * random.randint(1, 1000)))
         return '-'.join(re.findall(''.join('(\S{{{}}})'.format(l) for l in dashmap), random_val)[0])
    
    0 讨论(0)
提交回复
热议问题