I am searching for a keyword say "My system" So the readline will search for that keyword and copy the paragraph in which this keyword is present. I need to add '#" symbol before every line of the paragraph being printed. The code given below gives me just the first line added with "#".
z=open("ABC.txt","r")
for line in z.readlines():
if "My system" in line:
w.write('{}{}'.format('\n"#"',(line)))
How can i add "#" before every line of that particular paragraph. Please help
Keep a buffer (eg a list
) of lines that are part of the "current" paragraph as you are iterating through the lines. Set a flag when you find your keyword, and when you reach the next paragraph whitespace, print all of the buffered paragraph lines with or without your #
symbol. Otherwise, by the time you've matched the keyword the previous lines are "done" from your loop's perspective.
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.