Pull out a regular expression, for example:
text = "text\n text.....\nKeywords: key, key, key\n text......\n" import re match = re.search('(Keywords: .+?)\n', text) if match: print(match.group(1)) # "Keywords: key, key, key"
The brackets indicate the area you want to pull out. And the lines themselves inside the brackets can be obtained through group . If the search function is not found by the search function template, None will return, so it is advisable to break through the value.
In the regular schedule, we indicate that we are interested in a string starting with Keywords: after it, any characters before the \n character follow.
I will add the answer from the clarification in the comment:
import re data_list = ["text\n text.....\nKeywords: key, key, key\n key key key\n 1 text......\n"] for text in data_list: match = re.search('(Keywords: .+)\n', text, flags=re.DOTALL) if match: # b'Keywords: key, key, key\n key key key\n 1 text......' print(match.group(1).encode())
The result is output as a byte array so that you can see that the characters \n in place.