Posted on

A trick to use numpy.loadtxt on strings

numpy offers a function, numpy.loadtxt, to read in an array from a file. This is a extremely useful function especially when a 2d array is being read. However the function only accepts either a file path/name, or a file generator (file handler). You can not use a string directly as the argument.

numpy also offers a function numpy.fromstring to convert a string to an array, but the functionality is much more limited compared to numpy.loadtxt. Taking a simple example, if your string looks like

0.0 1.0e+02 2.0
3.0 4.0e-02 5.0

numpy.fromstring won’t do you any good. To solve this problem, you may use the io module.


The old StringIO module in Python 2 is now integrated into the new io module. The full documentation of io can be found here:
io module

io.StringIO creates a file stream, acceptable by the numpy.loadtxt now. So, supposedly your string above is saved as a variable var, all you need to do is,

import io
import numpy as np
f_handler = io.StringIO(var)
newarray = np.loadtxt(f_handler)

Notice that the stream has to be closed after use.

Posted on

How to enable Alt+Left click for column selection in PyCharm 如何在PyCharm 里设置Alt加左键实现列选中

Many of you are probably used to using Alt + left click to make column selections in editors like Atom, and if you are also using PyCharm you might be having a hard time finding where to set this up. Here, let me show you.

First of all, go to Preferences -> Keymap -> Editor Actions, find Create Rectangular Selection on Mouse Drag.
第一步,去Preferences -> Keymap -> Editor Actions,找到Create Rectangular Selection on Mouse Drag 选项。 Find the Editor Actions

Then you will find you won’t be able to directly add Alt + left click because it’s already occupied by another action: Add or Remove Caret. Personally I don’t use this much, so I just removed it.
然后你会发现你没法直接给其添加Add mouse shortcut, 因为Alt + left click 快捷方式已经被Add or Remove Caret 占用了。我个人不怎么用这个功能所以我直接在Add or Remove Caret把它删除了。如图所示。
Remove AltButton1 click from add or remove caret

Now you can add the mouse shortcut Alt + left click (in PyCharm the left click is called Button1 click) to Create Rectangular Selection on Mouse Drag.
现在你可以添加Alt left click (在PyCharm里left click是Button1 click).
Add Alt Button click

You should be able to see that in the menu now. Click on OK then you are ready to use Alt + left click to make column selections!
现在你应该能在菜单里看到这个快捷方式了。单击OK确定后,你就可以用Alt 加 左键来实现列选中啦!