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

2024-06-28 20:31:09 发布

您现在位置: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/”进行分割。如果我尝试拼接,则数据现在仅以空格分隔:

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']

为了删除我认为是问题所在的空行,我尝试了以下方法:How do I remove blank lines from a string in Python? 我知道这里也有类似的问题:用一个字符拆分一个字符串:Python: split a string by the position of a character

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


Tags: 数据intreesgroupsprintelmtulipoak
1条回答
网友
1楼 · 发布于 2024-06-28 20:31:09
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']

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

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

相关问题 更多 >