简体   繁体   中英

Remove a line from file NOT having a specific word in Python

Program in Python which,

remove a line starting with <div and ending with > but NOT having word text-align in between it.

Input:

<div>
<div><div>
<div style="text-align: center;">
<div kjgueberhfui;hh;ah>
<div kjfh fhefhufh  fhueshf >
<strong>PANKY</strong>
<div style="text-align: left;">

Output:

<div style="text-align: center;">
<strong>PANKY</strong>
<div style="text-align: left;">

I am searching for something like:

f1 = open('input.txt','r')
filedata = f1.read()
filedata = re.sub("<div[^>]*/^((?!text-align).)*$/[^>]*>","",filedata)

OR

f1 = open('input.txt','r')
for line in f1:
    if "<div" in line:
        if "text-align" in line;
        else:
            f1.write(line.replace(THIS_LINE,"")

The above 2 code are not working or incomplete!

f1 = open('input.txt','r')
for line in f1:
    if "<div" in line:
        if "text-align" in line;
        else:
            f1.write(line.replace(THIS_LINE,"")

Doesn't have statement after if, so it can't work. Besides you can combine two conditions:

with open('input.txt','r') as f1, open('output.txt', 'w') as f_out:
    for line in f1:
        if not ("<div" in line and "text-align" not in line):
            f2.write(line)

You can use startswith and endswith functions of a string.

    if line.startswith('<div') & line.endswith('>') & ('text-align' not in line):
        #do something

THANKYOU EVERYONE, because of you all I was able to figure out my answer,

some of your code output the reverse of what i asked for but here is the code which is the solution to my answer,

f1 = open('input.txt','r')
f2 = open('output.txt','w')

for line in f1:
    if "<div" in line and "text-align" in line:
        f2.write(line)
    if "<div" not in line:
        f2.write(line)

f1.close()
f2.close()

The more correct and complete answer to this question is something like this:

lines = open("input.txt").read().splitlines()

with open("input.txt", "w") as file:
    for line in lines:
        if not (line.startswith("<div") and "text-align" not in line and line.endswith(">")):
            file.write(line + "\n")

This would remove all unwanted lines from the input.txt

I hope this will help you:

with open('input.txt','rb+') as f1:    
    for div in f1:
          if 'text-align' in div or '<div' not in div:
                print(div)

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