Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to split python multi-space strings

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to split python multi-space string", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to split python multi-space string"!

Split python multi-space string

The problems to be dealt with in this article are:

A string is divided by multiple blanks of different lengths. I need to remove the spaces and leave useful information to form a list.

For example, "aa bbbbb ccc d"-"['aa',' bbbbb', 'ccc',' d']

Practice

1.str.split ()

2.filter (None,str.split ("))

It is not possible to directly use str.split (""). It can only split a space, as follows.

Def test_filter (): str = "aa bbbbb ccc d" str_list = str.split (") print str_list

Results:

The first approach

In fact, the split () function can split by space by default and delete the empty string in the result, leaving useful information

Def test_filter (): str = "aa bbbbb ccc d" str_list = str.split () print str_list

Results:

The second approach

You can filter split ("") with the filter function

Def test_filter (): str = "aa bbbbb ccc d" str_list = filter (None,str.split (")) print str_list

Results:

When the first parameter of filter is None, the non-null value of the second parameter is returned.

Perhaps the first method is more convenient.

How to split a string

There are few examples that show you how to split a string into lists in Python.

1. Split by SPAC

By default, split () uses spaces as delimiters.

Alphabet = "ab c d e f g" data = alphabet.split () # split string into a list for temp in data: print temp

Output quantity

A

B

C

D

E

F

G

two。 Segmentation + maximum segmentation

Split only by the first 2 spaces.

Alphabet = "ab c d e f g" data = alphabet.split ("", 2) # maxsplit for temp in data: print temp

Output quantity

A

B

C d e f g

3. Split with #

Another example.

Url = "mkyong.com#100#2015-10-1" data = url.split ("#") print len (data) # 3print data [0] # mkyong.comprint data [1] # 100print data [2] # 2015-10-1 for temp in data: print temp

Output quantity

three

Mkyong.com

one hundred

2015-10-1

Mkyong.com

one hundred

2015-10-1

Thank you for reading, the above is the content of "how to divide python multi-space string". After the study of this article, I believe you have a deeper understanding of how to split python multi-space string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 254

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report