Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract just the first line of multiline string

I am using split() to parse till a colon. I have several colons in my text but I just need the string from the first line. What i need to do to just get the first line?

line = """Hello : 
          This is a test ......:

          Testpath: C:\\...
          blablablabla
          123:"""

if ' :' in line: 
  av = line.split(" :",1)[0]
  print av

Is it possible to access the first line without using regexpression??

like image 291
erDi Avatar asked Oct 17 '25 18:10

erDi


1 Answers

If I understood your question correctly, you want to print only the first line of the multi-lined string irrespective of colon as separator. If this is the case, here is my possible solution (for windows os):

line = """ bravo cos
        daring in the blah blah."""

The soln is:

print(line.split("\n")[0])

Hope it helps.

like image 197
Chirag Dhyani Avatar answered Oct 19 '25 06:10

Chirag Dhyani