0

I am trying to use a code by someone else, but it keeps failing at one specific point. The code needs to find a multiline string in some inputfile and split the inputfile in two. The code seems logical to me, but I keep getting the same error. The inputfile looks something like this:

Text
more text 1
more text 2
VECTT
 0 0 0 0 0
more text 3
more text 4
more text 5

Here is a minimal working example:

vfile = open('inputfile','r').read()
vesta_end = vfile.split("VECTT\n 0 0 0 0 0")[1]
print(vesta_end)

I would expect to get the second part of the inputfile, so:

more text 3
more text 4
more text 5

Instead, I get following error:

Traceback (most recent call last):
  File "min.py", line 2, in <module>
    vesta_end = vfile.split("VECTT\n 0 0 0 0 0")[1]
IndexError: list index out of range

which, I believe, just means it did not recognize the intended string passed to the split function. Any ideas on how to make the function recognize the multiline string?

Jelle V
  • 21
  • 3
  • .split() is telling you that string is not in the file. Check spaces vs. tabs vs. non breaking spaces and \r vs. \n vs. \r\n – BoarGules May 08 '19 at 12:06
  • To further the previous comment: Your example code, when run against the example input you have provided, does exactly what you want: it splits the content into two parts. – larsks May 08 '19 at 12:19
  • \r\n did it! Didn't know about that... thanks! I guess this question is redundant then. – Jelle V May 08 '19 at 12:27

0 Answers0