如何在Python中的两个字符串之间过滤一段文本?
例如,我想过滤出以下文本中PreparedStatement preparedStatement =
connect.prepareStatement("INSERT into employee VALUES (default,?,?)", Statement.RETURN_GENERATED_KEYS);
preparedStatement.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime()));
preparedStatement.setString(2, "Test");
preparedStatement.executeUpdate();
ResultSet tableKeys = preparedStatement.getGeneratedKeys();
tableKeys.next();
和CLUSTERED BY
之间的所有行:
BUCKETS
结果应为:
CREATE TABLE some_name (
fv int,
sv int,
tv int)
CLUSTERED BY (fv,
sv,
tv)
SORTED BY (fv,
sv,
tv) INTO 2 BUCKETS;
-- more text afterwards
答案 0 :(得分:0)
您可以尝试以下操作:
start_word = "CLUSTERED"
end_word = "BUCKETS"
result_lines = []
with open(target_file, 'r') as f:
erasing = False
for line in f:
if not erasing and start_word in line:
// begin erasing lines
erasing = True
continue
if erasing and end_word in line:
// finished erasing lines
erasing = False
continue
if erasing:
// we are between the start and end of the section we want to erase
continue
else:
// either we haven't started erasing or we have already finished
result_lines.append(line)
print('\n'.join(result_lines))