使用关键字分隔符拆分大型文本文件



我正在尝试使用单词分隔符将大型文本文件拆分为较小的文本文件。我尝试搜索,但我只看到在 x 行之后分解文件的帖子。我对编程相当陌生,但我已经开始了。我想遍历所有行,如果它以 hello 开头,它会将所有这些行放入一个文件中,直到到达下一个 hello。文件中的第一个单词是 hello。最终,我试图将文本放入 R,但我认为如果我先像这样拆分它会更容易。任何帮助不胜感激,谢谢。

text_file = open("myfile.txt","r")
lines = text_file.readlines()
print len(lines)
for line in lines :
    print line
    if line[0:5] == "hello":

如果你正在寻找一个非常简单的逻辑,试试这个。

text_file = open("myfile.txt","r")
lines = text_file.readlines()
print len(lines)
target = open ("filename.txt", 'a') ## a will append, w will over-write
hello1Found = False
hello2Found = False
for line in lines :
    if hello1Found == True :  
        if line[0:5] == "hello":
            hello2Found = True
            hello1Found = False
            break ## When second hello is found looping/saving to file is stopped 
              ##(though using break is not a good practice here it suffice your simple requirement
        else: 
            print line #write the line to new file
            target.write(line)
    if hello1Found == False:
        if line[0:5] == "hello": ##find first occurrence of hello 
            hello1Found = True
            print line 
            target.write(line)      ##if hello is found for the first time write the 
                                ##line/subsequent lines to new file till the occurrence of second hello

我是Python的新手。我刚刚在东北大学完成了Python for Geographic Information Systems的课程。这就是我想出的。

import os
import sys
import arcpy
def files():
    n = 0
    while True:
        n += 1
        yield open('/output/dir/%d.txt' % n, 'w')
pattern = 'hello'
fs = files()
outfile = next(fs)
filename = r'C:outputdirfilename.txt'
with open(filename) as infile:
    for line in infile:
        if pattern not in line:
            outfile.write(line)
        else:
            items = line.split(pattern)
            outfile.write
            (items[0])
            for item in items:
                outfile = next(fs)
                outfile.write(item)
filename.close();outfile.close();

最新更新