python - 根据某些条件(在 Python 中)将字符串(行)添加到特定位置

标签 python python-3.x file

我有以下 .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 个选择:

  1. 使用file_obj.seek()(仅用于替换数据)
  2. 加载内存中的所有文件并将其转储
  3. 创建一个临时文件,您可以随时修改该文件,然后复制回原点

方案一好像下架了,因为要插入数据。 选项 2 似乎最适合您的情况,您只需要相应地调整代码(例如,使用字符串切片和连接而不是 read()write() ). 选项 3 也是可能的,但通常是更多的负担。但是,如果您无法将整个文件放入内存,它会特别有用。

为了完整起见,下面是每个选项的代码草图。


选项 1:

# file should be open as a binary to avoid messy offsets due to encoding
with open(filepath, 'rb+') as file_obj: 
    while True:
        line = file_obj.readline()
        if not line:  # reached end-of-file
            break
        if condition(line):  # for strings, use `line.decode()`
            position = file_obj.tell()
            offset = 0  # the offset from the beginning of the line
            file_obj.seek(position - len(line) + offset)  
            # new data must be `bytes`, for strings, use `new_data.encode()`
            file_obj.write(new_data)
            file_obj.seek(position)

选项 2:

with open(filepath, 'r+') as file_obj:
    text = file_obj.read()  # read the whole file
    ...                     # do your preprocessing on the text as string
    file_obj.seek(0)        # go back at the beginning of the file
    file_obj.truncate()     # disregard previous content
    file_obj.write(text)    # write data back

选项 3:

import shutil

with open(in_filepath, 'r') as in_file_obj, \
        open(out_filepath, 'w') as out_file_obj:
    for line in in_file_obj:
        # should actually reflect your logic here
        if must_insert_here():  
            # preprocess data to insert
            out_file_obj.write(new_line + '\n')

        # should actually reflect your logic here 
        if must_be_present_in_new():  
            out_file_obj.write(line)

# perhaps you actually want to use `copy2()` instead of `copy()`  
shutil.copy(out_filepath, in_filepath)      

关于python - 根据某些条件(在 Python 中)将字符串(行)添加到特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56275069/

相关文章:

python - 返回与出现次数、正则表达式相关的匹配

python - 其中哪些在 Python 中是不可变的?

处理 if-else 的 pythonic 方式

java - 如何从菜单项单击的目录加载下一个项目?

javascript - Gulp Concat 没有输出文件

python - spacy安装错误(运行cythonize失败)

python - 如何在 Odoo 8 中使用 fields_view_get?

c# - 如何读取将从 .Net Core API 中的表单上传的文件?

python - 对列表进行排序,其中某些值保持在固定位置

python - 使用上一列中的值填充 pandas 数据框中的 "None"值