是否可以将特定文本值放入基于文本的单个单元格中?例如,我有一些文本需要根据关键字"SAMPLE STORE"分隔并放入一个单元格中,

我try 了一些拆分函数,但没有给出预期的输出 例如:print(Contents.Split("示例故事"))

sample story 1:
    
This is another great story that teaches a lesson that’s written for kids but that adults can enjoy, too. The story tells of a grasshopper who lounges around all summer while his friend the ant prepares for the winter. When winter comes, the two friends end up in very different situations!
    
The moral is that those who save up during good times will get to enjoy the benefits when times are bad.
    
    
Sample Story 2:
    
This very short story from India was originally written in Sanskrit (an ancient language). When a group of doves is caught in a hunter’s net, they must work together as a team to escape from the hunter’s clutches.
    
You can listen to a reading of the story as you read along on this website.
   
    
sample story 4:
    
1. This is a modern-day story about a little girl with a big secret she can’t tell anyone about. When her teacher finds out her secret, they work together to fix the issue.
2. This modern story is about a young woman named Penny who is anxious about going to her family’s annual reunion barbecue. But despite screaming children and arguing cousins, Penny ends up happy that she came to the reunion when she starts a conversation with a handsome man.
Note:   
This story is a good choice for absolute beginners because it uses only the present tense. It’s also written in very basic English with simple vocabulary and short sentences.

Expected output

推荐答案

您可以try 使用regex来解析字符串(regex101):

import re
import pandas as pd

text = '''\
sample story 1:

This is another great story that teaches a lesson that’s written for kids but that adults can enjoy, too. The story tells of a grasshopper who lounges around all summer while his friend the ant prepares for the winter. When winter comes, the two friends end up in very different situations!

The moral is that those who save up during good times will get to enjoy the benefits when times are bad.


Sample Story 2:

This very short story from India was originally written in Sanskrit (an ancient language). When a group of doves is caught in a hunter’s net, they must work together as a team to escape from the hunter’s clutches.

You can listen to a reading of the story as you read along on this website.


sample story 4:

1. This is a modern-day story about a little girl with a big secret she can’t tell anyone about. When her teacher finds out her secret, they work together to fix the issue.
2. This modern story is about a young woman named Penny who is anxious about going to her family’s annual reunion barbecue. But despite screaming children and arguing cousins, Penny ends up happy that she came to the reunion when she starts a conversation with a handsome man.
Note:
This story is a good choice for absolute beginners because it uses only the present tense. It’s also written in very basic English with simple vocabulary and short sentences.'''

pat = re.compile(r'^(sample story \d+:)\s*(.*?)\s*(?=^sample story \d+|\Z)', flags=re.S|re.M|re.I)

out = pd.DataFrame(pat.findall(text), columns=['Header', 'Story'])
print(out)

打印:

            Header                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Story
0  sample story 1:                                                                                                                                                                                                                                             This is another great story that teaches a lesson that’s written for kids but that adults can enjoy, too. The story tells of a grasshopper who lounges around all summer while his friend the ant prepares for the winter. When winter comes, the two friends end up in very different situations!\n\nThe moral is that those who save up during good times will get to enjoy the benefits when times are bad.
1  Sample Story 2:                                                                                                                                                                                                                                                                                                                                                       This very short story from India was originally written in Sanskrit (an ancient language). When a group of doves is caught in a hunter’s net, they must work together as a team to escape from the hunter’s clutches.\n\nYou can listen to a reading of the story as you read along on this website.
2  sample story 4:  1. This is a modern-day story about a little girl with a big secret she can’t tell anyone about. When her teacher finds out her secret, they work together to fix the issue.\n2. This modern story is about a young woman named Penny who is anxious about going to her family’s annual reunion barbecue. But despite screaming children and arguing cousins, Penny ends up happy that she came to the reunion when she starts a conversation with a handsome man.\nNote:\nThis story is a good choice for absolute beginners because it uses only the present tense. It’s also written in very basic English with simple vocabulary and short sentences.

Python相关问答推荐

Pandas数据帧处理Pandas表中Json内的嵌套列表以获取后续Numpy数组

使用itertools出现第n个子串

想要使用Polars groupby_Dynamic来缩减时间序列收件箱(包括空垃圾箱)

当测试字符串100%包含查询字符串时,为什么t fuzzywuzzy s Process.extractBests不给出100%分数?

如何判断LazyFrame是否为空?

为什么我的(工作)代码(生成交互式情节)在将其放入函数中时不再工作?

理解Python的二分库:澄清bisect_left的使用

2维数组9x9,不使用numpy.数组(MutableSequence的子类)

@Property方法上的inspect.getmembers出现意外行为,引发异常

运行终端命令时出现问题:pip start anonymous"

大小为M的第N位_计数(或人口计数)的公式

log 1 p numpy的意外行为

NP.round解算数据后NP.unique

Telethon加入私有频道

cv2.matchTemplate函数匹配失败

Maya Python脚本将纹理应用于所有对象,而不是选定对象

在pandas/python中计数嵌套类别

OpenCV轮廓.很难找到给定图像的所需轮廓

Gekko中基于时间的间隔约束

使用字典或列表的值组合