按行拆分文件,并将第一个字符串保留为输出文件的标题。
创始人
2024-08-22 01:30:14
0

以下是一个示例代码,用于按行拆分文件并将第一个字符串保留为输出文件的标题:

def split_file_by_line(filename):
    with open(filename, 'r') as file:
        lines = file.readlines()
        
        for line in lines:
            # 获取每行的标题和内容
            title = line.split()[0]
            content = ' '.join(line.split()[1:])
            
            # 创建输出文件,以标题命名
            output_filename = f'{title}.txt'
            with open(output_filename, 'w') as output_file:
                output_file.write(content)
                
            print(f'Successfully split line into {output_filename}')


# 调用函数拆分文件
split_file_by_line('input.txt')

假设input.txt文件的内容如下:

Title1 This is the content for title 1
Title2 This is the content for title 2
Title3 This is the content for title 3

运行以上代码后,将会生成三个输出文件:

  • Title1.txt,内容为This is the content for title 1
  • Title2.txt,内容为This is the content for title 2
  • Title3.txt,内容为This is the content for title 3

每个输出文件的标题由输入文件的每行第一个字符串决定。

相关内容

热门资讯

透视ai代打!wepoke辅助... 透视ai代打!wepoke辅助德之星,wepoke透明挂辅助软件是真的吗,详细透视解密教程小薇(透视...
we辅助poker德之星!aa... we辅助poker德之星!aa扑克网上的挂真的(透视)好像存在有挂(详细辅助揭秘教程)是一款可以让一...
透视神器!WePoKe外 挂,... 透视神器!WePoKe外 挂,wopoker用ai有用吗,详细透视科技教程;1、上手简单,内置详细流...
wepoke有没有挂!德扑之星... wepoke有没有挂!德扑之星怎么设置(透视)起初真的是有挂(详细辅助新2025教程)是一款可以让一...
透视教程!wepoke辅助挂,... 透视教程!wepoke辅助挂,wepoke靠谱吗,详细透视透牌教程1、wepoke辅助挂系统规律教程...
aapoker透明挂!扑克时间... aapoker透明挂!扑克时间辅助(透视)一向是有挂(详细辅助可靠教程);是一款可以让一直输的玩家,...
透视计算!wepoke辅助有挂... 透视计算!wepoke辅助有挂,wepoke苹果版外挂,详细透视科技教程1、首先打开wepoke辅助...
wepoke ai辅助!德扑a... wepoke ai辅助!德扑ai助手软件(透视)原生存在有挂(详细辅助2025新版)1、不需要AI权...
透视中牌率!wepoke辅助,... 透视中牌率!wepoke辅助,we辅助poker,详细透视必备教程1、wepoke辅助机器人多个强度...
wpk发牌逻辑!微扑克ai代打... wpk发牌逻辑!微扑克ai代打(透视)好像有挂(详细辅助技巧教程),您好,wpk发牌逻辑这款游戏可以...