我有一些像这样拆分的文本:1号线hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph.\n, hey I'm a new paragraph. hey I'm a new paragraph. hey I'm a new paragraph.\n, hey im the third paragraph. hey im the third paragraph. hey im the third paragraph.\n2号线hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph.\n, hey I'm a new paragraph. hey I'm a new paragraph. hey I'm a new paragraph.\n, hey im the third paragraph. hey im the third paragraph. hey im the third paragraph.\n如您所见,这会持续一段时间。段落或行分组由“\ n”分隔,我怎样才能将所有段落放入一个列表中,其中没有更小的列表,例如:list = ["hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph.", "etc etc..."]
2 回答
手掌心
TA贡献1942条经验 获得超3个赞
假设您的文本在一个名为 的文件中file.txt,并且\n是一个字符串而不是转义字符,那么您可以简单地执行此操作:
full_list = []
file = open('file.txt', 'r')
for line in file.readlines():
full_list += [x.rstrip('\\n') for x in line.split('\\n,')]
file.close()
print(full_list)
噜噜哒
TA贡献1784条经验 获得超7个赞
这是你想要的?将字符串拆分为 \n 上的列表?
string = '''hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph.\n, hey I'm a new paragraph. hey I'm a new paragraph. hey I'm a new paragraph.\n, hey im the third paragraph. hey im the third paragraph. hey im the third paragraph.\n'''
string_list = string.split('\n')
print(string_list)
添加回答
举报
0/150
提交
取消