Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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中的某种模式将文件拆分为多个文件_Python_File - Fatal编程技术网

基于python中的某种模式将文件拆分为多个文件

基于python中的某种模式将文件拆分为多个文件,python,file,Python,File,文本文件如下所示: shfhsgfkshkjg gkjsfkgkjfgkfg model 1 lkgjhllghfjgh kjfgkjjfghg endmodel model 2 jfhkjhcgbkcjg xhbgkxfkgh endmodel 我想在每个模型和endmodel之间,新文件中的文本应该是什么。文件名应该像model1、model2……模型可能是100或更多。模型和数字之间的空格正好是8。请帮助我。这应该可以做到: def writeFiles(in

文本文件如下所示:

shfhsgfkshkjg
gkjsfkgkjfgkfg
model        1
lkgjhllghfjgh
kjfgkjjfghg
endmodel
model        2
jfhkjhcgbkcjg
xhbgkxfkgh
endmodel

我想在每个模型和endmodel之间,新文件中的文本应该是什么。文件名应该像model1、model2……模型可能是100或更多。模型和数字之间的空格正好是8。请帮助我。

这应该可以做到:

def writeFiles(infilepath):
    outfile = None
    with open(infilepath) as infile:
        for line in infile:
            line = line.strip()
            if line.startswith('model') and outfile is None:
                outfile = open(line, 'w')
                continue
            elif line == 'endmodel':
                outfile.close()
                outfile = None
                continue
            elif outfile is not None:
                outfile.write(line + '\n')
fileName = "text.txt"

inputfile = open(fileName, 'r').readlines()

files = {}
storeContent = False
content = ""
for line in inputfile:
   if "endmodel" in line.strip() and storeContent:
      files [storeContent] = content
      storeContent = False
   elif "model" in line:
      storeContent = line.replace(" ", "").strip()
   elif storeContent:
      if not content:
         content += line
      else:
         content += "\n" + line
   else:
      pass # ignore other content

for name, content in files.items():
   f = open(name + ".txt", "w")
   f.write(content)
   f.close

如果这对你不起作用,请告诉我。

闻起来有点像家庭作业,尤其是因为你已经做了。也许你应该试试。[发现了许多结果[()@inspectorG4dget:很好的发现。看起来他得到了写答案所需的所有信息,除了有人为他写了所有的代码……现在他也有了这些信息。@abarnert:谢谢你回复我,伙计!我认为这个答案更准确……我编辑了它。告诉我一件事,如果模型和数字之间的spcae正好是8个空格那么我应该怎么做才能准确地实现它呢?这个问题已经解决了。我想说很多thnx..它起了作用…你能解释一下你为什么使用os.path.join吗???thnx吗?我再次编辑了它..告诉我一件事,如果模型和数字之间的spcae正好是8个空格,那么我应该怎么做才能准确地实现它。我使用os.path.join,因为它只是一个好的函数惯例和处理每个操作系统的错误斜杠。此外,我不认为这会有任何问题。我只是尝试了一下,它对我来说很好。如果这个答案解决了你的问题,我可以请你投票表决。谢谢。 You can use below function to do this. This is based on below assumptions: 1. There are some junk characters before first model appears 2. after every endmodel there is model.

def file_read():
        path = 'D:\\'
        file = open(os.path.join(path,'dummy.txt'),'w')
        k = open('test.txt', 'r')
        count = 0
        for line in k.readlines():
            if line.startswith('endmodel'):
                continue
            if line.startswith('model'):
                count += 1
                file = open (os.path.join(path,'model' + str(count) + '.txt'),'w')
                continue
            file.write(line)