Search code examples
pythonpython-3.xsplitreverse

How to reverse individual lines and words in Python3 specifically working on each paragraph in a text file?


Basically, I have a text file: -

Plants are mainly multi-cellular. Green plants obtain most of their energy from sunlight via photosynthesis. There are about 320,000 species of plants. Some 260–290 thousand, produce seeds. Green plants produce oxygen.

Green plants occupy a significant amount of land today. We should conserve this greenery around us.

I wanted the output to be:-

oxygen. produce plants Green seeds. produce thousand, 260-290 Some plants. of species 320,000 about are There photosynthesis. via sunlight from energy their of most obtain plants Green multi-cellular. mainly are Plants

us. around greenery this conserve should we today. land of amount significant a occupy plants Green.

I used split() and then used .join() to combine the file, but it ended up reversing the whole thing and not paragraph-wise.


Solution

  • Change open("testp.txt") to open("[path to your file]")

    import re
    
    text = open("testp.txt").read()
    rtext = ""
    
    for p in re.split("\n", text):
        for w in reversed(re.split(" ", p)):
            rtext += w + " "
        rtext = rtext[:-1] + "\n"
    rtext = rtext[:-1]
    
    print(rtext)
    

    Update: this one is so simple:

    import re
    
    with open("testp.txt") as f:
        print("\n".join(
            " ".join(reversed(re.split(" ", p))) for p in re.split("\n", f.read())
        ))
    
    

    Update: code without using regex:

    with open("testp.txt") as f:
        print("\n".join(
            " ".join(reversed(p.split())) for p in f.read().splitlines()
        ))
    

    Note that you can use .split("\n") instead of .splitlines()

    The result for all versions is:

    Input:

    Plants are mainly multi-cellular. Green plants obtain most of their energy from sunlight via photosynthesis. There are about 320,000 species of plants. Some 260–290 thousand, produce seeds. Green plants produce oxygen.

    Green plants occupy a significant amount of land today. We should conserve this greenery around us.

    Output:

    oxygen. produce plants Green seeds. produce thousand, 260-290 Some plants. of species 320,000 about are There photosynthesis. via sunlight from energy their of most obtain plants Green multi-cellular. mainly are Plants

    us. around greenery this conserve should we today. land of amount significant a occupy plants Green