使用 slice() 但想使用开始和长度。这可能吗?

Using slice() but want to use start, and length. Is this possible?

我有将近一百列要从一个没有分隔符的大型机文件中分类,我正在使用切片功能来实现这一点。我将每个位置切片成一个列表,然后组合这些列表,然后创建一个 Pandas 数据框来执行额外的分析。

    a = slice(0,10)
    b = slice(10,25)

但是,我知道开始位置,以及每个字符串的长度。有没有类似的方法来使用开始和长度而不是开始和结束来分类?

你不能只使用 start+length 作为结束参数吗?

Python

>>> x = 'hello world'
>>> start = 6
>>> length = 5
>>> x[start:start+length]
'world'

Pandas

>>> import pandas as pd
>>> df = pd.DataFrame({'x':['hello world']})
>>> df['y'] = df['x'].str.slice(start, start+length)
>>> df
             x      y
0  hello world  world