How can you get the nth line of a string in Python 3? For example
getline(\"line1\\nline2\\nline3\",3)
Is there any way to do this
Use a string buffer:
import io def getLine(data, line_no): buffer = io.StringIO(data) for i in range(line_no - 1): try: next(buffer) except StopIteration: return '' #Reached EOF try: return next(buffer) except StopIteration: return '' #Reached EOF