按行拆分文件,并将第一个字符串保留为输出文件的标题。
创始人
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

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

相关内容

热门资讯

脚本推荐!aapoker免费透... 脚本推荐!aapoker免费透视脚本,aapoker真的假的(透视)都是是真的脚本(哔哩哔哩)1、下...
反观!wepokerplus万... 反观!wepokerplus万能挂,微乐小程序自建房透视,辅助透视(有挂教程)-哔哩哔哩1、实时we...
脚本总结!hhpoker辅助挂... 脚本总结!hhpoker辅助挂下载,hhpoker有没有辅助(透视)确实真的是有软件(哔哩哔哩)1、...
今年以来!wepoker黑侠辅... 今年以来!wepoker黑侠辅助器正版下载,微乐辅助脚本,解迷透视(有挂教学)-哔哩哔哩1、wepo...
围绕透视问题!aa poker... 围绕透视问题!aa poker透视软件,微乐智能辅助真的假的,专业脚本(有挂总结)-哔哩哔哩1、每一...
透视科普!aapoker插件下... 透视科普!aapoker插件下载,aapoker透视脚本(透视)原来是有挂(哔哩哔哩)1、这是跨平台...
透视软件!hhpoker必备开... 透视软件!hhpoker必备开挂,微信小程序微乐破解器2025,科普脚本(有挂技巧)-哔哩哔哩1)h...
脚本解密!德普之星辅助工具如何... 脚本解密!德普之星辅助工具如何打开,德普之星私人局辅助器(透视)其实是有技巧(哔哩哔哩)1、完成有辅...
现有说明如下!hhpoker软... 现有说明如下!hhpoker软件安装包,手机微乐小程序破解器如何使用,专业脚本(有挂讲解)-哔哩哔哩...
透视揭露!wpk控制牌是真的吗... 透视揭露!wpk控制牌是真的吗,wpk显示有作必弊(透视)都是真的有方法(哔哩哔哩)1、公共底牌简单...