根据 Python 中的字符将字符串分成两个不同长度的块

Slice a string into two chunks of different lengths based on character in Python

所以我有一个看起来像这样的文件:

oak
elm
tulip
redbud
birch

/plants/

allium
bellflower
ragweed
switchgrass

我只想将树木和草本植物分成两块,这样我就可以像这样分别称呼它们:

print(trees)
oak
elm
tulip
redbud
birch

print(herbs)
allium
bellflower
ragweed
switchgrass

正如您在示例数据中看到的那样,数据块的长度不等,因此我必须根据分隔符“/plants/”进行拆分。如果我尝试拼接,数据现在仅由 space:

分隔
for groups in plant_data:
    groups  = groups.strip()
    groups = groups.replace('\n\n', '\n')
    pos = groups.find("/plants/") 
    trees, herbs = (groups[:pos], groups[pos:])
print(trees)
oa
el
tuli
redbu
birc



alliu
bellflowe
ragwee
switchgras

如果我尝试简单地拆分,我会得到列表(这对我的目的来说没问题),但它们仍然没有分成两组:

for groups in plant_data:
    groups  = groups.strip()
    groups = groups.replace('\n\n', '\n')
    trees = groups.split("/plants/")
print(trees)
['oak']
['elm']
['tulip']
['redbud']
['birch']
['']
['', '']
['']
['allium']
['bellflower']
['ragweed']
['switchgrass']

为了删除我认为是问题所在的空行,我尝试了以下操作: 而且我知道这里类似地用字符拆分字符串:

但是我很困惑为什么我不能让这两个分开。

spam = """oak
elm
tulip
redbud
birch

/plants/

allium
bellflower
ragweed
switchgrass"""

spam = spam.splitlines()
idx = spam.index('/plants/')
trees, herbs = spam[:idx-1], spam[idx+2:]   
print(trees)
print(herbs)

输出

['oak', 'elm', 'tulip', 'redbud', 'birch']
['allium', 'bellflower', 'ragweed', 'switchgrass']

当然,除了使用 idx-1、idx+2 之外,您还可以使用不同的方法(例如列表理解)删除空 str

spam = [line for line in spam.splitlines() if line]
idx = spam.index('/plants/')
trees, herbs = spam[:idx], spam[idx+1:]