简体   繁体   English

如何删除python文件中所有带有未知单词的行?

[英]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. 我需要通过删除具有上述语法的所有字符串来修改和复制sql-file 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. 新列表(arr)包括除最后一个单词以外的所有单词。 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: 如果您使用的是Linux环境:

command: sed -i "DROP TABLE IF EXISTS TEST_TABLE;" 命令:sed -i“如果存在测试表,则删除表;” file_path 文件路径

ex: sed -i "DROP TABLE IF EXISTS TEST_TABLE;" 例如:sed -i“如果存在TEST_TABLE,则删除表;” data.txt data.txt中

For Mac: 对于Mac:

sed -i '' '/DROP TABLE IF EXISTS TEST_TABLE;/d' data.txt sed -i'''/如果存在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; DROP TABLE IF EXISTS any_table_name_here;将删除所有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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM