How to copy multiple input cells in Jupyter Notebook

前端 未结 5 1463
别跟我提以往
别跟我提以往 2021-02-05 05:12

Basically I want to copy (Ctrl+C) only the code portions from multiple cells without also copying the output or the In[1]: and Out[1]:

What is

相关标签:
5条回答
  • 2021-02-05 05:51

    Just do: File > Export Notebook As > Export Notebook to Asciidoc and it will be easy to copy paste. This is what an Asciidoc file looks like:

    +*In[ ]:*+
    [source, ipython3]
    ----
    import pandas as pd
    
    df = pd.read_csv("data/survey_results_public.csv")
    
    df.tail(10)
    
    df.shape
    
    pd.set_option("display.max_columns", 85)
    pd.set_option("display.max_rows", 85)
    
    schema_df = pd.read_csv("data/survey_results_schema.csv")
    
    schema_df.head(10)
    
    
    ----
    
    0 讨论(0)
  • 2021-02-05 06:01

    When you are on a cell in Command mode(blue color mode), simply press Shift + DownArrow or Shift + UpArrow to select multiple cells. Press ctrl + C. And that's it. You have copied your entire selected code at once. It doesn't affect whether you have cell outputs.

    Command mode: The Jupyter Notebook has two different keyboard input modes. Edit mode allows you to type code or text into a cell and is indicated by a green cell border. Command mode binds the keyboard to notebook level commands and is indicated by a grey cell border with a blue left margin.

    0 讨论(0)
  • 2021-02-05 06:01

    For jupyterlab after Shift + UpArrow or Shift + select with mouse on multiple cells. Right click on cells for copy(C) and paste(P).

    0 讨论(0)
  • 2021-02-05 06:13

    Open notebook dir as project in PyCharm, and then open the wanted ipynb file, select and copy all the source code, past into notepad++, replace "\r\n#%%\r\n\r\n" by null with extended search mode.

    0 讨论(0)
  • 2021-02-05 06:16

    In jupyter you can copy several cells or the content of one cell. If you follow @BenWS comment you can copy several cells, and if you do kernel > restart & clear outputs beforehand you woult not get the [out]. Shortcut is C for copy cell and V shift + V to paste below / above.

    However if you intend to copy several cells content, you should merge then before by select them and shift + M and then you can copy paste with ctrl + C.

    0 讨论(0)
提交回复
热议问题