Home > Enterprise >  How to remove certain lines from text file in python?
How to remove certain lines from text file in python?

Time:11-07

I am trying to delete specific lines from text files, Which is basically a java code in txt format. I am trying to delete multi-comments from the text, which means script should read the files and delete multi-comments starting with "/" and end with / along with "\\" , "package", and "import".

I am using this code, it worked last time for me, but now it is not working

Code

import re
import sys
import codecs
txt = "H:\APK_Source_Code_Merged\sstest1.txt"
with open(txt, 'r',encoding= 'utf-8', errors= 'ignore') as fil:
    file = fil.read()

wtalc = re.sub(r"//.*?", '', file, flags=re.S)
wtslc = re.sub(r"/*.*?*/", '', wtalc, flags=re.S)
wtblc = re.sub(r"import.*?", '', wtslc, flags=re.S)
wtblc = re.sub(r"package.*?", '', wtslc, flags=re.S)
print(wtblc)

Basically, it is a txt file. The above code perfectly work fine, if i read and remove certains lines from text string then it works fine. But if I read and remove from file, it does not remove any line at all. sstest1 = "

package net.youmi.android.b.a.e.c.g;

import net.youmi.android.b.a.e.a.b.g;
import net.youmi.android.b.a.e.a.b.i;
import net.youmi.android.b.a.e.a.b.l;
import org.json.JSONObject;
//asdasbdaiygd
/* loaded from: classes.dex */
public final class a extends l {
    /* JADX WARN: Failed to parse debug info
    java.lang.IllegalArgumentException: newPosition > limit: (67103403 > 2726956)
        at java.base/java.nio.Buffer.createPositionException(Buffer.java:318)
        at java.base/java.nio.Buffer.position(Buffer.java:293)
        at java.base/java.nio.ByteBuffer.position(ByteBuffer.java:1158)
        at java.base/java.nio.ByteBuffer.position(ByteBuffer.java:263)
        at jadx.plugins.input.dex.sections.SectionReader.absPos(SectionReader.java:82)
        at jadx.plugins.input.dex.sections.debuginfo.DebugInfoParser.process(DebugInfoParser.java:84)
        at jadx.plugins.input.dex.sections.DexCodeReader.getDebugInfo(DexCodeReader.java:118)
        at jadx.core.dex.nodes.MethodNode.getDebugInfo(MethodNode.java:546)
        at jadx.core.dex.visitors.debuginfo.DebugInfoAttachVisitor.visit(DebugInfoAttachVisitor.java:39)
     */
    //Override // net.youmi.android.b.a.e.a.b.l
    protected JSONObject a(i iVar, g gVar) {
        return null;
    }
} 

The above script works perfectly fine if I read and remove certain lines from the text string (from the variable). But if I read and remove it from the file, it does not remove any line at all.

CodePudding user response:

you have the wrong regex in order to expect "." or "*" as a string, you need to put a \ before it:

import re
import sys
import codecs
txt = "hi.txt"
with open(txt, 'r',encoding= 'utf-8', errors= 'ignore') as fil:
    file = fil.read()

wtalc = re.sub(r"//.*?\n", '', file)
wtslc = re.sub(r"/\*.*?\*/", '', wtalc, flags=re.S)
x = re.sub(r"import.*?\n", '', wtslc)
x = re.sub(r"package.*?\n", '', x)
print(x)

this should the trick.

CodePudding user response:

You just opened the file in the read mode and, assigned your changed data to a variable. You didn't modify the file yet. Now you write your data to file:

https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

  • Related