-2

I have a .txt file in which I have multiple lines of code which looks like this:-

[03-Jun-22 06:32 AM] flylineman#0052
9fSQR7M1aS95wtmDpsRTvzKJzbP49dngMVn58rG2Usxo


[03-Jun-22 06:32 AM] Doughnut#6155
AM3k8ggVkRgZrYfRCnon14wy2qtbso5HYRiynwvM5eFS


[03-Jun-22 06:33 AM] Antares#4605
7apq5QKC3bmVbkWRd5ke2J9JHyidjMervc7V1joTLCPp

I want to remove the first line and then every fourth line from the file (Lines with []).
Expected Output:-

9fSQR7M1aS95wtmDpsRTvzKJzbP49dngMVn58rG2Usxo

AM3k8ggVkRgZrYfRCnon14wy2qtbso5HYRiynwvM5eFS

7apq5QKC3bmVbkWRd5ke2J9JHyidjMervc7V1joTLCPp

OS:- Windows 10

I'm a beginner so I don't know much about how to remove these, and thus not able to make a code, it would be grateful if anyone could help me:)

Thanks!

Khushhal
  • 51
  • 8
  • 1
    Something like this would work: `with open('text_file.txt') as f: f.readlines()[1::4]` – Djib2011 Jun 06 '22 at 10:53
  • @Djib2011 I'm getting this error:- File "C:\Python310\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 410: character maps to – Khushhal Jun 06 '22 at 11:00
  • Maybe try a different encoding: `with open('text_file.txt', encoding="utf8") as f: f.readlines()[1::4]` – Djib2011 Jun 06 '22 at 11:03
  • @Djib2011 I' sorry to trouble you again, but it does nothing, not giving an error now but it doesn't change or delete any line from the file:/ – Khushhal Jun 06 '22 at 11:08

2 Answers2

3

You can use regular expressions:

import re

s ='''[03-Jun-22 06:32 AM] flylineman#0052
9fSQR7M1aS95wtmDpsRTvzKJzbP49dngMVn58rG2Usxo


[03-Jun-22 06:32 AM] Doughnut#6155
AM3k8ggVkRgZrYfRCnon14wy2qtbso5HYRiynwvM5eFS


[03-Jun-22 06:33 AM] Antares#4605
7apq5QKC3bmVbkWRd5ke2J9JHyidjMervc7V1joTLCPp'''

s_new = re.sub("\[.+\] .+\n","", s)  # remove label lines
s_new = [s for s in s_new.split("\n") if s != ""]  # remove empty lines
print(s_new, sep="\n\n")  # print with separation

# use this to write to a new file
with open("result.txt", "w") as f:
    for elem in s_new:
        f.write(elem)

Output:

9fSQR7M1aS95wtmDpsRTvzKJzbP49dngMVn58rG2Usxo

AM3k8ggVkRgZrYfRCnon14wy2qtbso5HYRiynwvM5eFS

7apq5QKC3bmVbkWRd5ke2J9JHyidjMervc7V1joTLCPp
Sati
  • 716
  • 6
  • 27
3

What about deleting every line that starts with "["?

with open("test.txt", "r") as f:
    lines = f.readlines()

with open("test.txt", "w") as f:
    for line in lines:
        if not "[" in line and line.strip() != "":
            f.write(line)
blunova
  • 2,122
  • 3
  • 9
  • 21
  • Thanks a lot, it worked!!! could you also tell me a way to remove all the empty lines?:) – Khushhal Jun 06 '22 at 11:14
  • @Khushhal: Store your required lines on a list and write back to your file, as shown in my answer below. – Sati Jun 06 '22 at 11:21
  • 1
    @Khushhal sorry, had lunch, I have edited my answer to remove also blank lines, you can do it in several ways, this is my suggestion. I used the `strip` function to remove white spaces from the strings. – blunova Jun 06 '22 at 11:56