您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用python将文本文件拆分为多个文本文件?

如何使用python将文本文件拆分为多个文本文件?

尝试re.findall()函数

import re

with open('input.txt', 'r') as f:
    data = f.read()

found = re.findall(r'\n*(A.*?\n\$\$)\n*', data, re.M | re.S)

[open(str(i)+'.txt', 'w').write(found[i-1]) for i in range(1, len(found)+1)]

前3次出现的 简约方法

import re

found = re.findall(r'\n*(A.*?\n\$\$)\n*', open('input.txt', 'r').read(), re.M | re.S)

[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found[:3]]

一些解释:

found = re.findall(r'\n*(A.*?\n\$\$)\n*', data, re.M | re.S)

将查找与指定RegEx匹配的所有匹配项,并将它们放入 ,称为found

[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found]

遍历(属于列表)所有元素(使用列表推导),found并为每个元素创建文本文件(称为“ index of the element + 1.txt”),并将该元素(出现)写入该文件

没有RegEx的另一个版本:

blocks_to_read = 3
blk_begin = 'A'
blk_end = '$$'

with open('35916503.txt', 'r') as f:
    fn = 1
    data = []
    write_block = False
    for line in f:
        if fn > blocks_to_read:
            break 
        line = line.strip()
        if line == blk_begin:
            write_block = True
        if write_block:
            data.append(line)
        if line == blk_end:
            write_block = False
            with open(str(fn) + '.txt', 'w') as fout:
                fout.write('\n'.join(data))
                data = []
            fn += 1

PS我个人不喜欢这个版本,我会使用RegEx

python 2022/1/1 18:36:54 有227人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶