简体   繁体   中英

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.

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

Read the file and use splitlines() to separate paragraphs. Then iterate the paragraphs, reversing the words.

with open("input.txt") as f:
    read_data = f.read().splitlines()

    for p in read_data:
        words = p.split()
        print(' '.join(reversed(words)))

To read and write to file you can do this:

with open("input.txt", 'r') as f:
    read_data = f.read().splitlines()

with open("output.txt", 'w') as fout:
    for p in read_data:
        words = p.split()
        fout.write(' '.join(reversed(words)))
        fout.write('\n')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM