i have some text that is split like so:
line 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.\n
line 2
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
this goes on for a while as you can see. the paragraphs or groupings of lines are split by "\\n," how can i get all the paragraphs into one list with no smaller lists inside for ex:
list = ["hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph. hey I'm a paragraph.", "etc etc..."]
Assuming your text is in a file named file.txt
, and that the \\n
is a string and not an escape character, then you can do this by simply:
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)
Is this what you are looking for? Splitting the string into a list on the \\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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.