Reputation: 65
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??
Upvotes: 4
Views: 7328
Reputation: 1063
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.
Upvotes: 8
Reputation: 6099
If you just want the first line you might want to use splitlines
If I understood well your question, this code might help you.
line = """Hello :
This is a test ......:
Testpath: C:\\...
blablablabla
123:"""
first, *others = line.splitlines()
first
will contain the first line, others
will contian a list of other lines
NB: You are not using any regex
Upvotes: 2