📅  最后修改于: 2023-12-03 14:43:37.240000             🧑  作者: Mango
Jupyter Notebook is a powerful tool for data analysis and scientific computing. However, sometimes we need to convert our notebook into a Python script. This can be done easily with the "File" -> "Download as" -> "Python (.py)" option. However, the resulting file will be cluttered with various lines of code that are not needed and can be distracting.
In this tutorial, we will learn how to save a Jupyter Notebook as a Python script without the terminal prompts and line numbers. This will help to clean up the script and make it more readable and presentable.
First, open the Jupyter Notebook that you want to convert to a Python script.
Before we proceed, it's important to save and checkpoint the Notebook. This ensures that any changes made during the conversion process can be undone if needed.
To save and checkpoint, go to "File" -> "Save and Checkpoint" or press "Ctrl + S".
Next, we will convert the Notebook to a Python script.
To do this, go to "File" -> "Download as" -> "Python (.py)".
This will download a Python script of the Notebook to your downloads folder.
Open the downloaded Python file in a text editor of your choice (e.g. Sublime Text, Atom, VS Code).
You will notice that there are various lines of code at the beginning and end of the file that are not needed. These lines include Jupyter Notebook prompts, imports, and function definitions.
We can remove these lines to clean up the script.
First, remove the Jupyter Notebook prompts. These are the lines that start with "In" and "Out". You can do this manually or by using a regular expression.
Next, remove any unnecessary imports. You can do this by commenting out the lines or deleting them entirely. Make sure to keep any imports that are necessary for the script to run.
Finally, remove any function definitions or other lines that are not needed for the script.
Once you have removed the unnecessary lines, save the file. You now have a cleaned up Python script of your Jupyter Notebook!
In this tutorial, we learned how to save a Jupyter Notebook as a Python script without the terminal prompts and line numbers. This makes the script more readable and presentable. We achieved this by saving the Notebook as a Python script, removing unnecessary lines from the script, and saving the cleaned up script.
Enjoy your clean and presentable Python script!