0

I wanted to know how to operate on alternate lines in a file (in particular I have a text consisting of a regular line and a line written backwards which I would reverse with [::-1]). I'll start with an example:

Easter eggs,
,eulb dna wolleY
Easter eggs,
.uoy dna em roF


Easter eggs,
,teews ydnaC
Easter eggs,
.tae ot doog erA

And I want to have:

Easter eggs,
Yellow and blue,
Easter eggs,
For me and you.


Easter eggs,
Candy sweet,
Easter eggs,
Are good to eat.

How can I do this? Because I would like to be able to do it also with empty lines (one or more of a new line, that is '\t') as in the example. All this by scrolling through a file. Thanks

EDIT: For @Mr. Hobo The error is in the last paragraph

ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ τροίης ἱερὸν πτολίεθρον ἔπερσεν:
πολλῶν δ' ἀνθρώπων ἴδεν ἄστεα καὶ νόον ἔγνω,
πολλὰ δ' ὅ γ' ἐν πόντῳ πάθεν ἄλγεα ὃν κατὰ θυμόν,
ἀρνύμενος ἥν τε ψυχὴν καὶ νόστον ἑταίρων.


ἀλλ' οὐδ' ὣς ἑτάρους ἐρρύσατο, ἱέμενός περ:
αὐτῶν γὰρ σφετέρῃσιν ἀτασθαλίῃσιν ὄλοντο,
νήπιοι, οἳ κατὰ βοῦς ὑπερίονος ἠελίοιο
ἤσθιον: αὐτὰρ ὁ τοῖσιν ἀφείλετο νόστιμον ἦμαρ.
τῶν ἁμόθεν γε,
θεά θύγατερ,
διός, εἰπὲ καὶ ἡμῖν.

,νορθελὄ νὺπἰα νογύφ ιοσὅ ,ςετνάπ νὲμ ιολλἄ 'θνἔ
νασσαλάθ ὲδἠ ςετόγυεφεπ ετ νόμελόπ ,νασἔ ιοκἴο
,ςὸκιανυγ ὲδἠ νονέμηρχεκ υοτσόν νοἶο 'δ νὸτ
νωάεθ αῖδ ὼψυλακ εκυρἔ 'ιντόπ ηφμύν
.ιανἶε νισόπ ηνέμοιαλιλ ,ισῖορυφαλγ ισσέπσ νἐ
  • what is the problem you are facing? can you post the code you have tried – deadshot Dec 23 '20 at 19:39
  • actually at the moment I was just forming the idea but, not being very experienced, I wanted to know if there was an "easy" way to do it. I had thought of putting a counter for the lines (obviously using an "if" first to see if the line starts with a character or is empty): this count was based on working on even lines – orsettomorbido Dec 23 '20 at 19:45
  • does the empty lines in output matters? – deadshot Dec 23 '20 at 19:47
  • The easy way: `for is_reversed, line in zip(itertools.cycle((False, True)), fileobj): if is_reversed: line = line.rstrip('\n')[::-1] + '\n'` – ShadowRanger Dec 23 '20 at 19:50
  • @deadshot I guess OP wants the blank line separately as he mentioned! – Mr. Hobo Dec 23 '20 at 19:51
  • yes, the empty lines matters – orsettomorbido Dec 23 '20 at 20:49

2 Answers2

1

Suppose you have the text in file.txt then you can do:

with open("file.txt") as f:
    content = f.readlines()
    
content = [value.strip()[::-1] if idx % 2 != 0 else value.strip() for idx, value in enumerate(content)]
print(content)

>> ['Easter eggs,', 'Yellow and blue,', 'Easter eggs,', 'For me and you.', '', '', 'Easter eggs,', 'Candy sweet,', 'Easter eggs,', 'Are good to eat.']

Explanation: I'm reading the file, as suggested in this community question and then, enumerating the contents to check if the file index is divisible by 2 (idx % 2 != 0).

EDIT: If you want the newline characters and want to paste the message in a single string, you can do this:

print('\n'.join(content))

Output:

Easter eggs,
Yellow and blue,
Easter eggs,
For me and you.


Easter eggs,
Candy sweet,
Easter eggs,
Are good to eat.
Mr. Hobo
  • 530
  • 1
  • 7
  • 22
  • @deadshot Yes, it does remove newline characters, keeps the blank lines as the OP has requested? – Mr. Hobo Dec 23 '20 at 19:56
  • Thanks, that worked for this text, but when I only have one blank line (instead of two as in the example), it no longer works. How can I do? – orsettomorbido Dec 23 '20 at 20:59
  • @orsettomorbido can you please share the error/output you are getting. I was able to get two blank lines as in the `output` I have shared. – Mr. Hobo Dec 24 '20 at 06:04
  • I edited the question so you can see what a problem it gives me – orsettomorbido Dec 24 '20 at 11:24
  • 1
    i think it gives error because odd and even lines swap if there is only a blank space. in fact I should only turn the even lines (but without counting the spaces because those swap even and odd lines every now and then) – orsettomorbido Dec 24 '20 at 11:30
  • I can't understand greek, but I think what you have pointed out is correct. It is not a question of doing operations on alternate lines. The last paragraph is giving a result opposite of what is required because it is working on alternate lines. – Mr. Hobo Dec 24 '20 at 17:34
  • in the last paragraph the program inverts the correct lines for me (and those already inverted remain like this). how can i fix? – orsettomorbido Dec 24 '20 at 18:57
0

Using readlines and while loop

f = open("demofile.txt", "r")
lines = f.readlines();
I = 0 # start lines index
Result = ""
while I < len(lines):
    line = lines[I]
    # start index if 0 so every reverse line will have odd index (1, 3, 5 etc).
    if not I % 2 == 0:
        Result += line.strip()[::-1] + '\n'
        I += 1
        continue
    Result += line
    I += 1

print(Result)
Karambit
  • 317
  • 4
  • 11
  • i dont know why but i tried to run it and it gets stuck in a loop. in particular it crashes me on "continue" – orsettomorbido Dec 23 '20 at 21:04
  • It should work now. I accidentally forgot to increment I – Karambit Dec 23 '20 at 21:21
  • new edit: this will work now, earlier i had used my mobile phone for answering thats why i accidentally used readline() instead of readlines() – Karambit Dec 24 '20 at 06:45
  • thanks, now it works, but it gives me a problem if after several paragraphs there is only one blank line (instead of two as in the example). That is, it reverses the even lines and leaves the odd ones turned – orsettomorbido Dec 24 '20 at 11:26
  • i think it gives error because odd and even lines swap if there is only a blank space. in fact I should only turn the even lines (but without counting the spaces because those swap even and odd lines every now and then) – orsettomorbido Dec 24 '20 at 11:29