0

I read a text file with several blocks (runs) separated by '\n\n' and am looking for the block with a certain string in the first line. My code:

    runinfo=""
    with open("./Project/Clean/indexfile.txt") as file:
        text=file.read()
        runs = text.split('\n\n') #split text into blocks for different runs
        for i in range(num_files):
            runinfotxt="There is no information to run_id "+Run_ids[i]+" in the indexfile"
            for block in runs: #search block with corresponding run_id
                firstline=""
                for let in block:
                    firstline=firstline+let
                    if let=="\n":
                        break
                print("FIRSTLINE: ",firstline)
                if firstline ==("run_id: "+Run_ids[i]+"\n"):
                    runinfotxt=block
                    runinfo=runinfo+block
                else:
                    pass

The problem: not the complete file is processed. 'text' includes the whole file. But when printing the length of the list 'runs' it returns 74, although my file has much more blocks.

Finding the correct block works well if it is within these first 74 blocks. Is there some kind of size limit that I am running into? I get no error messages. It just behaves as if the file was much shorter.

Edit: The number of items in 'runs' depends on the file that is read. I added some (about 15) blocks to the file and now the length of 'runs' is 94.

  • 1
    Is it possible your source file has some carriage returns (`\r`) instead of new lines? Do you get anything different if you change your delimiter to `\r\n`? – medic_dev May 11 '22 at 14:53
  • This is a good idea, but I think this is not the problem. I searched my file for "\r" with no result. – qcabepsilon May 11 '22 at 15:04
  • Have you tried just splitting the text by line rather than using `\n\n`: https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list – medic_dev May 12 '22 at 13:36
  • Splitting by lines works, but then the further processing takes longer. Thus I would much prefer to work with blocks. – qcabepsilon May 15 '22 at 16:45

0 Answers0