How can I format a txt file in python so that extra paragraph lines are removed as well as extra blank spaces? -
I'm trying to format a file like this: (random.txt)
Hi, I am trying to format a new txt document That extra space between words and paragraphs is only 1. It should show this txt document: So far the code I've managed has removed the empty space, but I have difficulty in recognizing where the new paragraph starts, which does not leave empty lines between the paragraphs, this is what I have done so far.
DIF RipplesSpace (input, output ): Ivar = open (Input, 'r') over = open (output, 'w') read n = ivar () ovar.write (''. Add (n.split ()) ivar.close () ovar.close () Edit:
I create an empty space between paragraphs The path is also found, but now it breaks every line and creates a place between the old line and the new line:
m = ivar.readlines () m [:] = [ If i am in i! = '\ N'] ovar.write ('\ n'.join (m))
You should process the input line through the line not only will simplify your program but will also be made easier on system memory.
The logic is the same (to connect with individual words and a single place) to normalize the horizontal white space in a line.
What you have to do for the paragraph is to check that line.strip () is empty (just use it as a Boolean expression) and keep a flag whether the previous The row was empty, you just throw empty lines but if you set a non-empty line and flags, print a single blank line before this. Open ('input.txt', 'R') as astr: new_par = istr: line = line.strip () wrong in the line: If not, then the row: # Blank new_par = Continue true if new_par: print () # Print a blank line print (''. Line.split ())) new_par = false But if you want to suppress empty rows, you will need an extra flag which you will set only after the first time the non-empty line
If you want to know more fancy, look at the module, but be aware that (or at least, what I can say) is bad bad case performance problems.
Comments
Post a Comment