Python 如何将字符串转换为按字符串分割的列表'\n\n\n\n\n';?
我有一个带文本的字符串,\n在其中。比如:Python 如何将字符串转换为按字符串分割的列表'\n\n\n\n\n';?,python,string,list,Python,String,List,我有一个带文本的字符串,\n在其中。比如: "Information moreinformation moreinformation \n\n\n\n\n Information moreinformation moreinformation \n\n\n\n\n Information moreinformation moreinformation" 现在我想要一个列表,由\n\n\n\n字符串分割。所以我想: ['Information moreinformation moreinform
"Information moreinformation moreinformation \n\n\n\n\n Information moreinformation moreinformation \n\n\n\n\n Information moreinformation moreinformation"
现在我想要一个列表,由\n\n\n\n字符串分割。所以我想:
['Information moreinformation moreinformation', 'Information moreinformation moreinformation', 'Information moreinformation moreinformation']
我已经试过了,但没用。那我还有一根绳子
with open ("file.txt", "r") as file:
content = file.readlines()
content = ' '.join(content)
content.split('\n\n\n\n\n')
print (content)
试试这个。否则,您不会更改内容变量的数据类型
试试这个。否则,您没有更改内容变量的数据类型。字符串是不可变的。字符串上的所有操作都返回结果,而不是修改参数。因此:
content=content.split(“…”)
起作用。谢谢!它起作用了:)介意发布一个文本文件的示例吗?…也许这可以用更好的方法解决,字符串是不可变的。字符串上的所有操作都返回结果,而不是修改参数。因此:content=content.split(“…”)
起作用。谢谢!它起作用了:)介意发布一个文本文件的示例吗?…也许可以用更好的方法解决这个问题
content = content.split('\n\n\n\n\n')