Python - Deleting the first 2 lines of a string

53,317

Solution 1

I don't know what your end character is, but what about something like

postString = inputString.split("\n",2)[2]

The end character might need to be escaped, but that is what I would start with.

Solution 2

x="""version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]
abc
asdda"""
print "\n".join(x.split("\n")[2:])

You can simply do this.

Solution 3

''.join(x.splitlines(keepends=True)[2:])

splitlines produces a list of strings. If keepends=True is given, line breaks are included in the resulting list l and ''.join(l) can be used to reproduce the original string.


Note that splitlines works well with a number of different line boundaries such as \u2028

>>> x = 'a\u2028b\u2028c\u2028'
>>> ''.join(x.splitlines(keepends=True)[2:])
'c\u2028'

while split('\n') fails in this case:

>>> x = 'a\u2028b\u2028c\u2028'
>>> x.split('\n',2)[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Also note that splitlines and split('\n') behave differently if they are called on an empty string or a string that ends with a newline character. Compare the following examples (copied from the documentation of splitlines):

>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']

>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']

However, if keepends=True is given, the trailing newline is preserved:

>>> "One line\n".splitlines(keepends=True)
['One line\n']

More examples and a list of what splitlines treats as a line boundary can be found here: https://docs.python.org/3/library/stdtypes.html?highlight=split#str.splitlines

Solution 4

Remove the lines with split:

lines = """version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]"""

lines = lines.split('\n',2)[-1]

Solution 5

I'd rather not split strings in case the string is large, and to maintain newline types afterwards.

Delete the first n lines:

def find_nth(haystack, needle, n):
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start+len(needle))
        n -= 1
    return start
assert s[find_nth(s, '\n', 2) + 1:] == 'c\nd\n'

See also: Find the nth occurrence of substring in a string

Or to delete just one:

s = 'a\nb\nc\nd\n'
assert s[s.find('\n') + 1:] == 'b\nc\nd\n'

Tested on Python 3.6.6.

Share:
53,317

Related videos on Youtube

Rontron
Author by

Rontron

Updated on June 19, 2021

Comments

  • Rontron
    Rontron almost 3 years

    I've searched many threads here on removing the first two lines of a string but I can't seem to get it to work with every solution I've tried.

    Here is what my string looks like:

    version 1.00
    6992
    [-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]
    

    I want to remove the first two lines of this Roblox mesh file for a script I am using. How can I do that?

    • sobolevn
      sobolevn almost 9 years
      your_string.split('\n')[2:]
  • Rontron
    Rontron almost 9 years
    Thanks, your answer was the clearest and easiest to understand.