python 剪切一行内容到另一个文件

2021-09-06 11:05:46 浏览数 (1)

自己做机器学习项目,手动打标签,完成后剪切一行数据到文件

参考内容:https://www.cnblogs.com/Dreamer-qiao/p/7390469.html

代码语言:javascript复制
# 剪切一行数据到另外一个文件
# 用途:自己做机器学习数据,打标签,标记source第一行数据的label后,剪切追加到 target 最后
source = "train_data.txt"
target0 = "train_data_0.txt"
target1 = "train_data_1.txt"


def del_line_from(file, del_line):  # del_line 行号从1开始
    with open(file, 'r', encoding="utf-8") as old_file:
        with open(file, 'r ', encoding="utf-8") as new_file:
            current_line = 0
            # 定位到需要删除的行
            while current_line < (del_line - 1):
                old_file.readline()
                current_line  = 1
            # 当前光标在被删除行的行首,记录该位置
            seek_point = old_file.tell()
            # 设置光标位置
            new_file.seek(seek_point, 0)
            # 读需要删除的行,光标移到下一行行首
            del_line_content = old_file.readline()
            # 被删除行的下一行读给 next_line
            next_line = old_file.readline()
            # 连续覆盖剩余行,后面所有行上移一行
            while next_line:
                new_file.write(next_line)
                next_line = old_file.readline()
            # 写完最后一行后截断文件,因为删除操作,文件整体少了一行,原文件最后一行需要去掉
            new_file.truncate()
    return del_line_content # 剪切的行的内容


import sys
while True:
    label = ""
    try:
        label = input()
    except:
        pass

    with open(target0, 'a', encoding='utf-8') as f0:
        with open(target1, 'a', encoding='utf-8') as f1:
            data = del_line_from(source, 1)
            if data == "":
                continue
            if label == "":
                if data[-2] == "0":
                    f0.write(data) # 将剪切的行,追加写入目标文件
                else:
                    f1.write(data)
            elif label == "1":
                if data[-2] == "0":
                    data = data[:-2] "1n"
                f1.write(data)
            elif label == "0":
                if data[-2] == "1":
                    data = data[:-2] "0n"
                f0.write(data)
            else:
                print("------输入有误-------")
                print(data)

另外,可以用 sublime text 安装 Auto Refresh 插件,自动刷新文档显示 在 cmd 窗口输入 python ***.py 就可以标注了:

代码语言:javascript复制
直接回车,接受原来的标签
输入 0, 或者 1 标注当前数据标签

0 人点赞