Python3: 使用 Dateutil 从一个长字符串中生成多个变量

Python3: Use Dateutil to make multiple vars from a long string

我有一个程序,我想从一首歌中随机抽出一行,然后将它们与其他歌曲的其他歌词串在一起。通过查看,我发现 dateutil 库可能能够帮助我从一个字符串中解析多个变量,但它并没有完全满足我的要求。

我有多个这样的字符串,只是更长。

"This is the day\nOf the expanding man\nThat shape is my shade\nThere where I used to stand\nIt seems like only yesterday\nI gazed through the glass\n..."

我想从这个字符串中随机拉出一行(到分页符)并将其保存为一个变量,但是在多个字符串上迭代它,任何帮助将不胜感激。

假设您想从字符串中随机抽取一行,您可以使用 random 模块中的 choice

random. choice ( seq ): Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

from random import choice
data = "This is the day\nOf the expanding man\nThat shape is my shade\nThere where I used to stand\nIt seems like only yesterday\nI gazed through the glass\n..."
my_lines = data.splitlines()
my_line = choice(my_lines)
print(my_line)

输出

That shape is my shade