繁体   English   中英

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

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

我有一个像这样的字符串:

DROP TABLE IF EXISTS TEST_TABLE;

我需要通过删除具有上述语法的所有字符串来修改和复制sql-file 假设表的名称可能会更改,并且在其他行中可能会有所不同。 如何仅知道语法就删除该行?

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)

尝试此操作,因为您想保留序列中的最后一个单词:

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)

新列表(arr)包括除最后一个单词以外的所有单词。 例:

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

得到:

TEST_TABLE

据我从您的代码了解。

如果您使用的是Linux环境:

命令:sed -i“如果存在测试表,则删除表;” 文件路径

例如:sed -i“如果存在TEST_TABLE,则删除表;” data.txt中

对于Mac:

sed -i'''/如果存在TEST_TABLE,则删除表; / d'data.txt

据我所知,您应该使用正则表达式:

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);

DROP TABLE IF EXISTS any_table_name_here;将删除所有DROP TABLE IF EXISTS any_table_name_here; 并输出:

 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 ]

我建议分开阅读各行,并删除所有以上述语法开头的行。 使用此功能,您可以输入文件并更改要删除的语法。 但是您当然可以复制逻辑并直接输入文件名。

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))

输入:

#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')

输出:

#testfile2.sql
IT
WORKS

暂无
暂无

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

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