newline in text extraction from pdf
Question: I am coding a function about extracting text in pdf, I am also using the
[pyPdf](https://pypi.python.org/pypi/pyPdf) library. Extracting was okay. But
I am encountering a couple of problems like it excluding the newline.

So I find a way to add a newline, so I have done this:

    
    
    # Iterate pages
    for i in range(0, pdf.getNumPages()):
        # Extract text from page and add to content
        content += pdf.getPage(i).extractText()
        content = content.replace('. ', '. <br />')
        pages += content
    
    # Collapse whitespace
    content = " ".join(pages.replace(u"\xa0", " ").strip().split())
    

The problem is even instances like this:

    
    
    1. Apple
    

became like this:

    
    
    1.
    
    Apple
    

Which it shouldn't be. I just want to add newline on every end of a sentence.

Is there a way to check or determine when the sentence ends? Or checking
whether it is as numbering?


Answer: A hackish solution is to perform replacement only if the full stop is not
immediately preceded by a digit. Change the line `content = content.replace('.
', '. <br />')` to the following:

    
    
    import re
    
    re.sub(r'([^0-9])\. ', r"\1. <br />", content)
    

