Merge the lines between triple single quotes and then replace the starting quote with '#'

Merge the lines between triple single quotes and then replace the starting quote with '#'


'''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very
important practical

'''



Required Output:


#Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical



(I want to count it as a comment)



My Code:
import re


START_PATTERN = "'''n"
END_PATTERN = "'''n"

with open('counter.py') as file:
match = False
newfile = None
for line in file:
if re.match(START_PATTERN, line):
match = True
newfile = open('my_new_file.txt', 'w')
continue
elif re.match(END_PATTERN, line):
match = False
newfile.close()
continue
elif match:
newfile.write(line)
newfile.write('n')



This just writes the last multilines comment in the file. Not all.





What have you tried, and what are you stuck on?
– John Kugelman
Aug 30 at 12:13





Why is the output expecting a split before the word frequency counter?
– Sayse
Aug 30 at 12:16





Its not split, its supposed to be all in the same line.
– nehaj
Aug 30 at 12:17





Why do you want to do this? Unlike docstrings, comments aren't available to help etc.
– jonrsharpe
Aug 30 at 12:26


help





They aren't just comments. They're docstrings. Maybe adding some real context to the question would help?
– jonrsharpe
Aug 30 at 14:01




4 Answers
4



You can read the whole file in and apply an re.sub in multiline mode.
Afterwards (or before, doesn't mater...) just crop those three ' and add #:


re.sub


'


#


import re
with open('test.py', 'r') as f:
txt = f.read()
print('IN:n', txt)
txt = re.sub('n', ' ', txt, flags=re.MULTILINE)
txt = '#' + txt[3:-3]
print('nOUT:n',txt)


IN:
'''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very
important practical

'''

OUT:
# Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical





Thanks it worked. Can you explain the "flags = re.MULTILINE"
– nehaj
Aug 30 at 12:36





Normally re works on a line by line base, so you just can't match newlines. This mode makes re handle the complete text as one block. See also docs.python.org/3/library/re.html#re.MULTILINE
– SpghttCd
Aug 30 at 12:38



re


re



You can use re.sub to replace one or more new-lines (n) with a singular space. Then strip the result (of any trailing and leading spaces) and concatenate that onto a '#':


re.sub


n


'#'


import re
'#' + re.sub('n+',' ',s).strip()
#'#Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical'





Correct, even if I'd replace 'n' by 's', so that two lines do not stick too close together...
– SpghttCd
Aug 30 at 12:15



'n'


's'





@SpghttCd My bad, I will update to use a regex
– Joe Iddon
Aug 30 at 12:23





It doesn't concatenate the lines in one line.
– nehaj
Aug 30 at 12:25





@nehaj see the answer now; is it not exactly the same as your desired?
– Joe Iddon
Aug 30 at 12:26





it puts the '#' in front of each line. But doesn't concatenate the lines into one.
– nehaj
Aug 30 at 12:29




Replace n with . Here is a code:


n



a = '''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very
important practical

'''

print("#"+a.replace("n"," "))



Output:


# Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical



Using str.join


str.join



Ex:


s = '''
Created on Mar 11, 2017

@author: XXZ
This file is to demonstrate the word frequency counter. This is a very
important practical

'''
print("# "+ " ".join(i.strip() for i in s.split()) )



Output:


# Created on Mar 11, 2017 @author: XXZ This file is to demonstrate the word frequency counter. This is a very important practical



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế