Adding line breaks in ipython

2019-01-21 04:14发布

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?

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-21 04:24

Been suffering this problem for a while. Just found that Cntrl-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

查看更多
够拽才男人
3楼-- · 2019-01-21 04:27

The %edit magic function in iPython lets you edit code in your favorite editor and will then execute it as if it was typed directly. You can also edit code you've already typed into the repl since it's stored in a special variable, for example:

In [1]: def foo(x):
   ...:     print x
   ...:     
In [2]: %edit _i1
查看更多
甜甜的少女心
4楼-- · 2019-01-21 04:35

There is also a way to add a newline directly in the repl: ctrl-v, ctrl-j

The ctrl-v basically lets you send a control code and then the ctrl-j is the code for a newline (line-feed). It's a bit awkward to type but has the advantage of also working in the regular Python shell as well as in Bash itself.

Edit: At least in iTerm2, you can assign it to a single hotkey as well. I set ctrl-enter to "Send hex codes" of 0x16 0x0a. Could also use cmd-enter or whatever else.

查看更多
5楼-- · 2019-01-21 04:35

A easy way of doing it is using the ;\ operator. ; to signal that its the end of a command and \ to indicate the the command follows in a new line as follows:

In[4]: password = "123";\
       username = "alpha"

This will let you have multiple line commands in ipython without invoking the editor

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-21 04:36

For completeness: newer IPython versions (0.11+) have a very nice graphical console which allows you to navigate your code with the arrows and even reposition the cursor with the mouse.

In multi-line statements, some keys take on a special function (arrows to navigate, Enter to insert a line break and others). You'll have to position the cursor at the end of the last line of the statement in order to get default behaviour, e.g. get the Up arrow to mean "previous statement" instead of "move the cursor to the previous line". Or get Enter to mean "execute code" instead of "insert line break in the middle of code".

The documentation on it is a bit sparse and fragmented in different pages, so here are the essential three links for getting started with it:

  1. Intro to the Qt Console
  2. Configuring IPython using nice per-user profile files instead of command line arguments

    You are interested in the ipython_qtconsole_config.py

  3. How to get an ipython graphical console on Windows 7?

查看更多
干净又极端
7楼-- · 2019-01-21 04:36

Type Ctrl+q then Enter. As other pointed out, Ctrl+q lets you send a character code, but hitting Ctrl+q then Enter may be more natural than Ctrl+q then Ctrl+j.

Another option is to type ( then Enter, write, and delete the ( afterwards. This solution is similar to the one where you to type ;\ to say the statement is not completed.

查看更多
登录 后发表回答