简体   繁体   中英

How to delete all line with some unknown words in file on python?

I have a string like:

DROP TABLE IF EXISTS TEST_TABLE;

I need to modify and copy sql-file by deleting all strings with the above syntax. It is assumed that the name of the table may change and in other lines, it may be different. How can I delete this line knowing only the syntax?

with open(r"D:\testfolder\input.sql", 'r') as file_in:
    text = file_in.read()
    text = text.replace("DROP TABLE IF EXISTS ", "")
with open(r"D:\testfolder\t2.sql", 'w') as file_out:
    file_out.write(text)

Try this for you want to keep the last word in the sequence:

with open(r"D:\testfolder\t2.sql", 'w') as file_out:
    with open(r"D:\testfolder\input.sql", 'r') as file_in:
        text = file_in.read()
        arr = text.split()[-1]
        file_out.write(arr)

The new list (arr) includes all the words except the last one. Example:

text = 'DROP TABLE IF EXISTS TEST_TABLE'
arr = text.split()[-1]
print arr

gives:

TEST_TABLE

As I understood from your code.

If you are using linux environment:

command: sed -i "DROP TABLE IF EXISTS TEST_TABLE;" file_path

ex: sed -i "DROP TABLE IF EXISTS TEST_TABLE;" data.txt

For Mac:

sed -i '' '/DROP TABLE IF EXISTS TEST_TABLE;/d' data.txt

You should use regex as far as I understand:

import re

str = "DROP TABLE IF EXISTS table_name; OTHER STUFF OTHER STUFF OTHER STUFF";

result = re.sub(r'DROP TABLE IF EXISTS .*\;', '', str); # Use this instead of replace()
print(result);

This will remove all DROP TABLE IF EXISTS any_table_name_here; and output:

 OTHER STUFF OTHER STUFF OTHER STUFF
import re

#### file 'infopanel.ver' is for example only !
## lines_list = ['Info-Panel V1.2\n', 'Machinebrand: Vu+ \n', 'Machinename: Solo SE \n', 'oem name: vuplus \n', 'Boxtype: vusolose \n', 'Keymap: /usr/share/enigma2/keymap.xml \n']
## lines_str = 'Info-Panel V1.2\nMachinebrand: Vu+ \nMachinename: Solo SE \noem name: vuplus \nBoxtype: vusolose \nKeymap: /usr/share/enigma2/keymap.xml \n'

with open('/tmp/infopanel.ver','r') as f:
    lines_str = f.read()
result = re.sub(ur'.*?Machine.*?', '', lines_str)

with open('/tmp/infopanel.ver','r') as f:
    lines_list = f.readlines()
result = [ line for line in lines_list if 'Machine' not in line ]

I would suggest reading the lines seperately, and deleting all the lines that start with the mentioned syntax. With this function, you can enter your files and change the Syntax you want to delete as well. But you can of course just copy the logic and enter your filenames directly.

def clear_file(file1, file2, syntax='DROP TABLE IF EXISTS'):
    with open(file1, 'r') as file_in:
        new_lines = [line for line in file_in.readlines() if not line.startswith(syntax)]
    with open(file2, 'w') as file_out:
        file_out.write(''.join(new_lines))

Input:

#testfile1.sql
DROP TABLE IF EXISTS TEST_TABLE
IT
DROP TABLE IF EXISTS TEST_2_table.table hello world
DROP TABLE IF EXISTS TABLE foo_bar_baz.tablexyz
WORKS

>>> clear_file('testfile1.sql', 'testfile2.sql')

Output:

#testfile2.sql
IT
WORKS

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