whitespace in regular expression

\t is not equivalent to \s+, but \s+ should match a tab (\t). The problem in your example is that the second pattern \s\s+ is looking for two or more whitespace characters, and \t is only one whitespace character. Here are some examples that should help you understand: \s\s+ would also match ‘ \t’, ‘\n\t’, ‘ \n \t \t\n’. Also, \s\s* is equivalent to \s+. Both will match one or more whitespace … Read more

How to move a file in Python?

os.rename(), os.replace(), or shutil.move() All employ the same syntax: Note that you must include the file name (file.foo) in both the source and destination arguments. If it is changed, the file will be renamed as well as moved. Note also that in the first two cases the directory in which the new file is being created must … Read more

numpy matrix vector multiplication

Simplest solution Use numpy.dot or a.dot(b). See the documentation here. This occurs because numpy arrays are not matrices, and the standard operations *, +, -, / work element-wise on arrays. Note that while you can use numpy.matrix (as of early 2021) where * will be treated like standard matrix multiplication, numpy.matrix is deprecated and may … Read more

How can I split and parse a string in Python?

“2.7.0_bf4fda703454”.split(“_”) gives a list of strings: This splits the string at every underscore. If you want it to stop after the first split, use “2.7.0_bf4fda703454”.split(“_”, 1). If you know for a fact that the string contains an underscore, you can even unpack the LHS and RHS into separate variables: An alternative is to use partition(). … Read more