0

Best way to remove all characters of a string until new line character is met python?

str = 'fakeline\nfirstline\nsecondline\nthirdline'

into 

str = 'firstline\nsecondline\nthirdline'
user3388884
  • 4,748
  • 9
  • 25
  • 34

3 Answers3

7

Get the index of the newline and use it to slice the string:

>>> s = 'fakeline\nfirstline\nsecondline\nthirdline'
>>> s[s.index('\n')+1:] # add 1 to get the character after the newline
'firstline\nsecondline\nthirdline'

Also, don't name your string str as it shadows the built in str function.

Edit:

Another way (from Valentin Lorentz's comment):

s.split('\n', 1)[1]

I like this better than my answer. It's splits the string just once and grabs the latter half of the split.

Community
  • 1
  • 1
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
  • I was going to say that `find` is better; if `\n` isn't found, `index` raises an exception, whereas `find` will at least return a sensical value in this case. (Why wouldn't you expect to "remove" the empty string from the original if there is no newline?) – chepner Jun 06 '14 at 20:49
  • @chepner: I initially used `find`, but changed it to `index` because I don't know what the desired behavior is on a single line string. Is that single line a "fake line" or not? I think a noisy fail is warranted in this case. – Steven Rumbalski Jun 06 '14 at 20:50
  • I think it would depend on what the OP wants to do if there are no newlines. I was assuming you'd want the original string back, but I can see an argument for deleting the entire string instead. – chepner Jun 06 '14 at 20:53
  • Thanks for all the suggestions! I ended up using find because i'm not expecting an response (remote server) without any new lines... if if such condition is met... i'm in trouble :) – user3388884 Jun 06 '14 at 20:55
  • 2
    `s.split('\n', 1)[1]` does it in one step – Valentin Lorentz Jun 06 '14 at 20:57
  • @chepner: I basically wrote exactly the opposite of your comment to start with, back when it was `find`. :^) Noise is good, esp. when the desiderata are underspecified. – DSM Jun 06 '14 at 21:51
  • @DSM Your comment is what prompted me to start providing an argument in favor of `find`, and then Steven agreed with you and change it before I could finish the comment :) – chepner Jun 06 '14 at 22:03
  • @ValentinLorentz Calling `split` creates a temporary list, simply to obtain a single element, that the posted answer does not require. – chepner Jun 06 '14 at 22:04
  • @chepner: A temporary two item list is unlikely to be much of a performance drag. – Steven Rumbalski Jun 08 '14 at 19:20
1

str.split("\n") gives a list of all the newline delimited segments. You can simply append the ones you want with + afterwards. For your case, you can use a slice

newstr = "".join(str.split("\n")[1::])
Magitrek
  • 545
  • 5
  • 19
1

You can use re.sub() ( regular expression replacement ) as well.

>>> import re
>>> s = 'fakeline\nfirstline\nsecondline\nthirdline'
>>> re.sub(r'^.*\n', '', s)
'firstline\nsecondline\nthirdline'
hwnd
  • 69,796
  • 4
  • 95
  • 132