Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding line breaks in ipython

Tags:

python

ipython

If introduce a for loop in iPython, or any multi-line command, how do I go back and add lines to it? I ran this:

for row in table.find_all('tr'):     cells = row.find_all('td')     for c,cell in enumerate(cells):         print c,":",cell.get_text().strip()     try:         this = cells[0]         that = cells[1]         the_docket = cells[2]         other_thign = cells[3]         jumble = re.sub('\s+',' ',str(cells[5])).strip()                 except:         "Nope" 

And realized I need to add a line to it, but I can't just hit "enter" in iPython, b/c that runs the command. So can I edit that multi-line command w/in iPython?

like image 865
Amanda Avatar asked Nov 23 '12 22:11

Amanda


People also ask

How do I start a new line in IPython?

If you're having trouble adding newlines in the middle of a multiline code block in IPython, try using Ctrl-V Ctrl-J to insert a linebreak at the cursor. It will not execute the code block like hitting Enter will.

How do I type multiple lines in IPython?

ctrl+o. If you type ctrl+o, you should be able to add additional lines to the input to run them simultaneously. If that doesn't work, as a workaround, you can paste multiple lines after copying them from elsewhere, or you can press enter on a line with a semicolon or unclosed parentheses.

How do you break a line in Jupyter?

Just add <br> where you would like to make the new line.

How do I add a line to a jupyter notebook?

The easiest way to add line numbers to a Jupyter Notebook is to use the keyboard shortcut, which is Ctrl-m to enter Command Mode, then type L. Just highlight the cell you are interested in adding line numbers to, then hit the keyboard shortcut to toggle the line numbers.


1 Answers

Been suffering this problem for a while. I just found that when using Ctrl-qCtrl-j (That's lowercase Q, J, no need to hold the shift key) will add a linefeed to an existing IPython edit session.

for li in some_list: print(li)     

Moving the cursor after the colon and pressing Ctrl-qCtrl-j

for li in some_list: print(li) 

IPython: 5.2.1, iTerm2: 3.0.15, macOS: 10.12.6

like image 87
JS. Avatar answered Oct 11 '22 11:10

JS.