[PYTHON] [Addition] A memo for dividing a character string containing multiple spaces

Splitting a string containing multiple spaces

For example, if the following character string is divided by half-width spaces, an empty character string will be included in the element if there are multiple half-width spaces in the middle.

test1.py


time = "Dec  3 14:25:33"
words = time.split(" ")
print words #['Dec', '', '3', '14:25:33']

Solved by using a regular expression module.

test2.py


import re

time = "Dec  3 14:25:33"
words = re.split(" +", time)
print words #['Dec', '3', '14:25:33']

Postscript

As you pointed out in the comment, if you omit the split argument, it seems that you can split with delimiters such as spaces, tabs, and line breaks without entering an empty string.

reference

[Python practice book](http://php6.jp/python/basics/%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%97/%E3%83%A2 % E3% 82% B8% E3% 83% A5% E3% 83% BC% E3% 83% AB /)

Recommended Posts

[Addition] A memo for dividing a character string containing multiple spaces
[Linux] When you want to search for a specific character string from multiple files
Outputs a line containing the specified character string from a text file
Is it a character string operation?
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
[Python] How to invert a character string
[Python beginner memo] Python character string, path operation
Get a Unicode JSON string containing Japanese.
Memo for creating a text formatting tool
String conversion of a list containing numbers
[Tentative] How to convert a character string to Shift_jis with kivy-ios Memo kivy v1.8.0