Python - Deleting the first 2 lines of a string

73.7k views Asked by At

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?

8

There are 8 answers

1
dstudeba On BEST ANSWER

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.

0
Daniel On

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]
0
vks On
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.

0
Bharadwaj On

You could use some rules, like consider those lines only if they start with '[' character lines = [line for line in lines if line.startswith('[')]

0
Ciro Santilli OurBigBook.com On

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.

0
Saeed Zahedian Abroodi On

You can find index of '\n' and ignore first; Then, start new string from end of second '\n' sub-string in main string.

import re


def find_sub_string_index(string, sub_string, offset=0, ignore=0):
    start = 0
    swap = len(sub_string)
    ignore += 1 # find first at least
    try:
        if start < 0:
            return -1 # Not Found
        if offset > 0:
            # Start main string from offset (offset is begining of check)
            string = string[offset:]
        for i in range(ignore):
            # swap: end of substring index
            # start is end of sub-string index in main string
            start += re.search(sub_string, string).start() + swap
            string = string[start:]
        return start
    except:
        return -1 # Got Error


string = """The first line.
The second line.
The third line.
The forth line.
The fifth line."""
sub_string = "\n"

ignore = 1 # Ignore times

start = find_sub_string_index(string, sub_string, ignore=1)

print("Finding sub-string '{0}' from main text.".format(sub_string))
print("Ignore {0} times.".format(ignore))
print("Start index:", start)
print("Result:")
print(string[start:])

The result is:

$ python3 test.py
Finding sub-string '
' from main text.
Ignore 1 times.
Start index: 33
Result:
The third line.
The forth line.
The fifth line.
$
$
$
$ python3 test.py
Finding sub-string 'The' from main text.
Ignore 2 times.
Start index: 19
Result:
 second line.
The third line.
The forth line.
The fifth line.
$
0
lambdakappatheta On
''.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

0
Ahmad Masalha On

This works for me:

first_line = text.find('\n') + 1
second_line = text.find('\n', first_line) + 1
text = text[second_line:]