Python - remove last line from line

I am trying to capture and manage data in a Telnet session using telnetlib, everything is going well, however my newbie with Python causes me some headache.

My problem is pretty straightforward, I can capture and display the data I want (for now), but I just seem to loop through the errors cyclically when I try to remove the last row of data from the data that I captured. My code looks something like this:

... Snipped Boring Stuff ... tn.write('command to gather data' + '\r') data = tn.read_very_eager() print data ... snipped more boring stuff ... 

Pretty simple ... So, how in the world do I delete the last row of data from tn.read_very_eager () or data ()?

Any direction would be amazing ... Sorry for the very simple question, but the material that I read and tried so far did not do anything, but upset me, my keyboard can no longer be abused :)

+6
source share
1 answer

You can delete the last line of a line as follows:

 def remove_last_line_from_string(s): return s[:s.rfind('\n')] string = "String with\nsome\nnewlines to answer\non StackOverflow.\nThis line gets removed" print string string = remove_last_line_from_string(string) print '\n\n'+string 

The output will be:

 >>> String with some newlines to answer on StackOverflow. This line gets removed String with some newlines to answer on StackOverflow. >>> 
+11
source

Source: https://habr.com/ru/post/953426/


All Articles