Is there an easy way of removing blank lines from IPython notebook?
I have picked up a habit of blank lines from web development and my fingers tend to hit enter automatically. This makes IPython notebooks less(by taking too much of my 14" screen), not more readable in most cases.
I was wondering if there is a way of automatically remove blank lines from the notebooks.
In notebooks, I think preferred way of splitting the code is by placing each separate method to a different cell.
Is commonly accepted style guide for notebooks or does PEP 8 apply as it is?
EDIT: I think question 2, Is answered by IPython docs. https://github.com/ipython/ipython/wiki/Dev:-Coding-style
If you are talking about deleting the empty line from jupyter notebook which opens in web browser then press Esc and D(keyboard key) 2 times.
You can also see all the keyboard shortcuts in the jupyter notebook file
by clicking on the help and then from dropdown on keyboard shortcuts.
Pressing Esc and D (2 times) will delete only blank lines starting from the beginning. It won't delete any lines which have some text or code.
Pressing Esc and D (2 times) deletes the complete cell.
I guess the Question is about deleting the blank lines, unless the author interpretation of blank lines are blank cells.
If you came here looking for "How to delete a single line (whether code line or blank line) in a jupyter notebook cell"
Ans: ctrl-d (in Edit mode)
Using a Mac:
to forward delete a line from the curser use Fn + command + delete
to backward delete a line from the curser use command + delete
to delete a whole cell use Esc + (d twice)
These, and other shortcuts, can be found under the Help tab.
Go to the required line and press: ESC + D (together twice)
Use Ctrl+Backspace or Ctrl+Delete both will delete blank lines.
Ctrl+Backspace can be used to delete whole text of line before cursor position.
Similarly Ctrl+Delete to delete text after cursor.
If you came here looking for "How to delete a single line (whether code line or blank line) in a jupyter notebook cell"
ctrl-d (in Edit mode)
I think the question is about deleting an empty new line of a cell.My solution is to write something in the line you want to delete, then go to the end of the line above it and press delete till the junk written in the line to be deleted is cleared.This deletes the empty new line as well.
Select the row you want to delete and click the scissors icon highlighted in the below screenshot -
Related
whenever I press the letter/button 'U' during coding in vs code, it automatically creates a new file.
the button 'u' is working as a shortcut key to create a new file in vs code.
lastly, if I copy/paste the letter 'u' in my code then it works fine.
I think some of your keybinds are messed up, You could try to check here, see if this works.
https://code.visualstudio.com/docs/getstarted/keybindings#:~:text=All%20keyboard%20shortcuts%20in%20VS,This%20will%20open%20your%20keybindings.
This should be due to some extensions you installed, or some misoperation that led to the modification of shortcut keys.
Find the keyboard shortcut and search for "creat: new file"
How can I move a line (or set of selected lines) down or up in jupyter notebook. Are there any shortcuts?
After moving line 3 one line up:
For example ALT+UP or ALT+DOWN is for up/down displacement that is used in Eclipse IDE.
This question discusses cell displacement.
TLDR:
By following this tutorial, I was able to enable all the sublime-text shortcuts (which includes the moving lines with Ctrl+Up/Ctrl/Down).
I then was able to furthermore, customize the shortcuts by editing the sublime.js
Complete answer:
Find your jupyter path with jupyter --config-dir
Add the following code to <JUPYTER-PATH>/custom/custom.js:
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
// setTimeout(function(){ // uncomment line to fake race-condition
cell.Cell.options_default.cm_config.keyMap = 'sublime';
var cells = IPython.notebook.get_cells();
for(var cl=0; cl< cells.length ; cl++){
cells[cl].code_mirror.setOption('keyMap', 'sublime');
}
// }, 1000)// uncomment line to fake race condition
}
);
You need to find this codemirror/keymap/sublime.js (that is required in the code from point 2), for me, it was in <MY-PYTHON-ENVIRONMENT>/lib/python3.8/site-packages/notebook/static/components/codemirror/keymap/sublime.js
At the end of this file, you can edit the shortcuts, in the keyMap.pcSublime section of this file (if you have a PC).
For OP, the lines to edit would be: swapLineUp and swapLineDown
I did this with Ubuntu & sublime installed.
You can select the line with mouse in jupyter notebook. Drag it
up or down before the line where you want to move it. Press >
button on keyboard while your text is still selected.
This will bring the cursor to end of selected line,
but before the text of line being replaced. Press Enter.
Since jupyter works with code blocks you can use that shortcut between code blocks as
Ctrl + Shift + -
But this will only works between code cells not in the same code block. I don't think there is a built-in shortcut for that. For more shortcuts you can look here: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
There's no such a shortcut in Jupyter, however, you can manage your own shorcuts Go to Help > Edit keyboard Shortcuts and start setting your commands.
There's some good info on this other post which will lead you to manage your profile's notebook.json file and so on.
Hope this was useful ;)
Edit: typo
You can create your custom shortcuts in Jupyter notebook
Jupyter Shortcuts
I am using Python 2.7 with Pycharm and I am working on a quite large text files; they are about 3gb in total.
I need to run LDA, PoS tagging, and other feature extraction methods on the data from the file but everytime I test my code, it has to read the file and go through the same process all over again from the beginning.
This is why I often use Jupyter because all the data / variables in previous cells are kept in memory.
Is there any way to do something similar with Pycharm?
For instance, let's say I am adding features to do_some_feature_extraction()
def do_some_feature_extraction(str_list):
# feature extraction 1
# feature extraction 2
str_list = []
with open("some_file.txt", "rb") as f_in:
for line in f_in:
str_list.append(line)
do_some_feature_extraction(str_list)
Let's say, there was an error on "feature extraction 1" and then I fixed it.
Then I will run the code again, then there will be another error on "feature extraction 2". Then I will fix it and run the code again from the beginning.
Instead of doing this, can I just set some sort of checkpoint before executing do_some_feature_extraction(str_list) ?
click the left side of your code ... next to the line number(or where the line number would be if you have them turned off)
a red dot should appear (this is called a breakpoint)...
now run it in debug mode
when you reach your breakpoint you can click the console tab
and then click the interactive terminal button(>_) to work directly with the context of the program
This is my first time using this so be kind :) basically my question is I am making a program that opens many Microsoft Word 2007 docs and reads from a certain table in that document and writes that info to an excel file there is well in excess of 1000 word docs. I have all of this working but the only problem when I run my code it does not close MSword after opening each doc I have to manually do this at the end of the program run by opening word and selecting exit word option from the Home menu. Another problem is also if a run this program consecutively on the second run everything goes to hell it prints the same thing repeatedly no matter which doc is selected I think this may have to do with how MSword is deciding which doc is active e.g. is it still opening the last active document that was not closed from the last run. Anyways here is my code for the opening and closing part I wont bore you guys with the rest::
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0
# Open a specific file
#myWordDoc = tkFileDialog.askopenfilename()
MSWord.Documents.Open("C:\\Documents and Settings\\fdosier" + chosen_doc)
#Get the textual content
docText = MSWord.Documents[0].Content
charText = MSWord.Documents[0].Characters
# Get a list of tables
ListTables = MSWord.Documents[0].Tables
------Main Code---------
MSWord.Documents.Close
MSWord.Documents.Quit
del MSWord
Basically, Python is not VBA, so this:
MSWord.Documents.Close
is equivalent to:
getattr(MSWord.Documents, "Close")
i.e. you just get some method object and do nothing with it. You need to call the method with the call operator (the parentheses :) :
MSWord.Documents.Close()
Accordingly for .Quit.
Before your MSWord.Quit did you try using:
MSWord.ActiveWindow.Close
Or even more simpley just doing
MSWord.Quit
I dont really understand if you are trying to close a document or the application.
I think you need a MSWord.Quit at the end (before and/or instead of the the del)
In the actual window where I right code is there a way to insert part of the code into everyline that I already have. Like insert a comma into all lines at the first spot>?
You need a file editor, not python.
Install the appropriate VIM variant for your operating system
Open the file you want to modify using VIM
Type: :%s/^/,/
Type: :wq
If you are in UNIX environment, open up a terminal, cd to the directory your file is in and use the sed command. I think this may work:
sed "s/\n/\n,/" your_filename.py > new_filename.py
What this says is to replace all \n (newline character) to \n, (newline character + comma character) in your_filename.py and to output the result into new_filename.py.
UPDATE: This is much better:
sed "s/^/,/" your_filename.py > new_filename.py
This is very similar to the previous example, however we use the regular expression token ^ which matches the beginning of each line (and $ is the symbol for end).
There are chances this doesn't work or that it doesn't even apply to you because you didn't really provide that much information in your question (and I would have just commented on it, but I can't because I don't have enough reputation or something). Good luck.
Are you talking about the interactive shell? (a.k.a. opening up a prompt and typing python)? You can't go back and edit what those previous commands did (as they have been executed), but you can hit the up arrow to flip through those commands to edit and reexecute them.
If you're doing anything very long, the best bet is to write your program into your text editor of choice, save that file, then launch it.
Adding a comma to the start of every line with Python:
import sys
src = open(sys.argv[1])
dest = open('withcommas-' + sys.argv[1],'w')
for line in src:
dest.write(',' + line)
src.close()
dest.close()
Call like so: C:\Scripts>python commaz.py cc.py. This is a bizzare thing to do, but who am I to argue.
Code is data. You could do this like you would with any other text file. Open the file, read the line, stick a comma on the front of it, then write it back to file.
Also, most modern IDEs/text editors have the ability to define macros. You could post a question asking for specific help for your editor. For example, in Emacs I would use C-x ( to start defining a macro, then ',' to write a comma, then C-b C-n to go back a character and down a line, then C-x ) to end my macro. I could then run this macro with C-x e, pressing e to execute it an additional time.