我有以下.sql文件:
execute_all.log
set echo on
SET SQLBLANKLINES ON
@@2019-03-26_DX_1.sql
@@2019-05-10_DX_2.sql
@@2019-05-10_DX_3.sql
@@2019-05-14_1600_DX_4.sql
@@2019-05-21_0900_DX_5.sql
@@2019-05-21_0900_DX_6.sql
@@2019-05-21_0900_DX_7.sql
@@2019-05-21_0900_DX_8.sql
SET SQLBLANKLINES OFF
spool off;
@@make_constraint.sql
从“@@”开始的所有内容都是与我相关的文件名。 在列表中,我还有以下文件:
skripts_to_deploy = ['2019-05-14_1600_DX_4.sql','2019-05-15_1500_DX_55.sql']
标准如下:
skripts_to_deploy
中文件的日期(在@之后的部分)小于下一行中的日期,则将该行(文件名)添加到此位置(并保留其他行,但如果需要,可以移动)。你知道吗代码如下:
path = "C:\\Users\\danyef"
skripts_to_deploy = ['2019-05-14_1600_DX_13.sql','2019-05-15_1500_DX_55.sql']
level = 'DXIDS'
with open(level + "_EXECUTE_ALL.sql","r+") as file:
for line in file:
if line == "execute_all.log\n" or line == "set echo on\n" or line == "SET SQLBLANKLINES ON\n":
continue
for skript in skripts_to_deploy:
if '@@' + skript in line:
continue
next_line = next(file)
print(next_line)
if next_line == 'SET SQLBLANKLINES OFF':
file.write('@@' + skript + '\n')
print("written SET SQLBLANKLINES OFF:",skript)
else:
next_line = datetime.strptime((next_line.split('_')[0]).split('@@')[1],'%Y-%m-%d')
if datetime.strptime(skript.split('_')[0],'%Y-%m-%d')<= next_line:
file.write('@@' + skript + '\n')
print("written:",skript)
重要提示:next_line = datetime.strptime((next_line.split('_')[0]).split('@@')[1],'%Y-%m-%d')
只是从现有文件的行中提取日期。你知道吗
在我的代码中,它添加了一行,但是不是在正确的位置(基于上面提到的标准)而是在文件的末尾添加。你知道吗
也许我这边的人漏了,欢迎你改正。 提前谢谢你。你知道吗
编辑:预期输出:
execute_all.log
set echo on
SET SQLBLANKLINES ON
@@2019-03-26_DX_1.sql
@@2019-05-10_DX_2.sql
@@2019-05-10_DX_3.sql
@@2019-05-14_1600_DX_4.sql
**@@2019-05-15_1500_DX_55.sql**
@@2019-05-21_0900_DX_5.sql
@@2019-05-21_0900_DX_6.sql
@@2019-05-21_0900_DX_7.sql
@@2019-05-21_0900_DX_8.sql
SET SQLBLANKLINES OFF
spool off;
@@make_constraint.sql
大多数文件系统不支持就地插入数据。你知道吗
通常,您有3个选项:
file_obj.seek()
(仅用于替换数据)选项1似乎已下架,因为您希望插入数据。 选项2似乎最适合您的情况,您只需要相应地调整代码(例如,使用字符串切片和串联,而不是
read()
和write()
)。 方案3也可以,但一般来说负担更重。但是,如果无法在内存中容纳整个文件,则它特别有用。你知道吗为了完整起见,下面是每个选项的代码草图。你知道吗
选项1:
选项2:
选项3:
相关问题 更多 >
编程相关推荐