Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据内容将txt文件拆分为多个文件_Python_Text_Extract - Fatal编程技术网

Python 如何根据内容将txt文件拆分为多个文件

Python 如何根据内容将txt文件拆分为多个文件,python,text,extract,Python,Text,Extract,我在使用python时遇到问题。 我有一个txt文件,里面有500篇论文的500篇摘要, 我想做的是把这个txt文件分成500个文件,每个txt文件只包含一个摘要。 现在,我发现,对于每个摘要,末尾都有一行,以“PMID”开头,所以我想用这一行拆分文件。 但我对python真的很陌生。 有什么想法吗? 提前谢谢 txt文件如下所示: 1. Ann Intern Med. 2013 Dec 3;159(11):721-8. doi:10.7326/0003-4819-159-11-20131203

我在使用python时遇到问题。 我有一个txt文件,里面有500篇论文的500篇摘要, 我想做的是把这个txt文件分成500个文件,每个txt文件只包含一个摘要。 现在,我发现,对于每个摘要,末尾都有一行,以“PMID”开头,所以我想用这一行拆分文件。 但我对python真的很陌生。 有什么想法吗? 提前谢谢

txt文件如下所示:

1. Ann Intern Med. 2013 Dec 3;159(11):721-8. doi:10.7326/0003-4819-159-11-201312030-00004.  
text text text texttext texttext texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext text
PMID: 24297188  [PubMed - indexed for MEDLINE]

2. Am J Cardiol. 2013 Sep 1;112(5):688-93. doi: 10.1016/j.amjcard.2013.04.048. Epub 
2013 May 24.
text texttext texttext texttext texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext texttext texttext texttext texttext text
PMID: 23711805  [PubMed - indexed for MEDLINE]

3. Am J Cardiol. 2013 Aug 15;112(4):513-9. doi: 10.1016/j.amjcard.2013.04.015. Epub 
2013 May 11.
text texttext texttext texttext texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext texttext texttext texttext texttext text
PMID: 23672989  [PubMed - indexed for MEDLINE]

等等。

有很多方法可以做到这一点。这里有一个方法。 如果数据位于名为
data
的文件中:

import re

def open_chunk(readfunc, delimiter, chunksize=1024):
    """
    http://stackoverflow.com/a/17508761/190597
    readfunc(chunksize) should return a string.
    """
    remainder = ''
    for chunk in iter(lambda: readfunc(chunksize), ''):
        pieces = re.split(delimiter, remainder + chunk)
        for piece in pieces[:-1]:
            yield piece
        remainder = pieces[-1]
    if remainder:
        yield remainder

with open('data', 'r') as infile:
    chunks = open_chunk(infile.read, delimiter=r'(PMID.*)')
    for i, (chunk, delim) in enumerate(zip(*[chunks]*2)):
        chunk = chunk+delim
        chunk = chunk.strip()
        if chunk:
            print(chunk)
            print('-'*80)
            # uncomment this if you want to save the chunk to a file named dataXXX
            # with open('data{:03d}'.format(i), 'w') as outfile:
            #     outfile.write(chunk)
印刷品

1. Ann Intern Med. 2013 Dec 3;159(11):721-8. doi:10.7326/0003-4819-159-11-201312030-00004.  
text text text texttext texttext texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext text
PMID: 24297188  [PubMed - indexed for MEDLINE]
--------------------------------------------------------------------------------
2. Am J Cardiol. 2013 Sep 1;112(5):688-93. doi: 10.1016/j.amjcard.2013.04.048. Epub 
2013 May 24.
text texttext texttext texttext texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext texttext texttext texttext texttext text
PMID: 23711805  [PubMed - indexed for MEDLINE]
--------------------------------------------------------------------------------
3. Am J Cardiol. 2013 Aug 15;112(4):513-9. doi: 10.1016/j.amjcard.2013.04.015. Epub 
2013 May 11.
text texttext texttext texttext texttext texttext texttext texttext texttext text
text texttext texttext texttext texttext texttext texttext texttext texttext text
PMID: 23672989  [PubMed - indexed for MEDLINE]
--------------------------------------------------------------------------------
取消最后两行的注释,将块保存到单独的文件中


为什么这么复杂?

对于短文件,您可以简单地将整个文件读入一个字符串,然后使用正则表达式拆分该字符串。上面的解决方案是对该思想的改编,它可以处理大文件。它以块的形式读取文件,找到分割块的位置,并在找到块时返回块

处理由分隔符regex模式分隔的文件块的问题经常出现。因此,与其为每个文件编写定制的解决方案,不如更容易地使用像
open\u chunk
这样的实用功能,它可以处理所有此类问题,而不管使用什么分隔符,并且处理方式比处理大文件和小文件都要简单。

您可以尝试:

with open("txtfile.txt", "r") as f:  # read file
    ss = f.read(-1)

bb = ss.split("\nPMID:")  # split in blocks

# Reinsert the `PMID;`, if nedded:
bb1 = bb[:1] + [ "PMID:" + b  for b in bb]

请注意,每个块中的最后一条换行将被删除。这些块可以写入单独的文件。

你能发布一个文件示例吗?谢谢,更新了@robertdejonggethank@unutbu!