博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python字符串拆分
阅读量:2530 次
发布时间:2019-05-11

本文共 5342 字,大约阅读时间需要 17 分钟。

Python string split() function is used to split a string into the list of strings based on a delimiter.

Python字符串split()函数用于根据定界符将字符串拆分为字符串列表。

Python字符串拆分 (Python String split)

Python string split() function syntax is:

Python字符串split()函数语法为:

str.split(sep=None, maxsplit=-1)

sep argument is used as the delimiter. If the contains consecutive delimiters, then an empty string is returned. Delimiter argument can be of multiple characters too.

sep参数用作定界符。 如果包含连续的定界符,则返回一个空字符串。 分隔符参数也可以是多个字符。

If the delimiter is not provided or None, then whitespaces are considered as the delimiter. In this case, no empty string will be returned in case there are leading or trailing white spaces. Also, multiple whitespaces will be considered as a single delimiter.

如果未提供定界符或None ,则将空格视为定界符。 在这种情况下,如果存在前导或尾随空格,则不会返回任何空字符串。 同样,多个空格将被视为单个定界符。

If maxsplit is provided, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits and all possible splits are returned in the list.

如果提供了maxsplit,则最多完成maxsplit个拆分(因此,列表中最多包含maxsplit + 1个元素)。 如果未指定maxsplit或-1,则对拆分数没有限制,并且所有可能的拆分都将返回列表中。

Python字符串split()示例 (Python String split() example)

Let’s look at a simple example where a string will be split into a based on the specified delimiter.

让我们看一个简单的示例,在该示例中,将根据指定的分隔符将字符串拆分为 。

s = 'Python is Nice'# simple string split examplestr_list = s.split(sep=' ')print(str_list)

Output:

输出:

['Python', 'is', 'Nice']

字符串split()与maxsplit示例 (String split() with maxsplit example)

s = 'Python is Nice'str_list = s.split(sep=' ', maxsplit=1)print(str_list)

Output: ['Python', 'is Nice']

输出: ['Python', 'is Nice']

Notice that returned list has only 2 items, the string was split only once.

请注意,返回的列表只有2个项目,字符串仅被拆分了一次。

未提供sep或无 (sep is not provided or None)

s = '  Java  Python iOS    Android  'str_list = s.split()print(str_list)

Output: ['Java', 'Python', 'iOS', 'Android']

输出: ['Java', 'Python', 'iOS', 'Android']

The leading and trailing whitespaces are ignored in the returned list. Also, consecutive whitespaces are also considered as a single delimiter.

返回列表中忽略前导和尾随空格。 同样,连续的空格也被视为单个定界符。

多行字符串拆分示例 (Multiline string split example)

multiline_str = 'Hi There\nHow are you?\nI am fine'multiline_str_split_list = multiline_str.split(sep='\n')for s in multiline_str_split_list:    print(s)

Output:

输出:

Hi ThereHow are you?I am fine

多字符分隔符示例 (Multi-Character separator example)

s = 'Hi||Hello||Adios'str_list = s.split('||')print(str_list)

Output: ['Hi', 'Hello', 'Adios']

输出: ['Hi', 'Hello', 'Adios']

str.split()函数示例 (str.split() function example)

We can use split() function directly from str class too.

我们也可以直接从str类使用split()函数。

print(str.split('ABACAD', sep='A'))print(str.split('ABACAD', sep='A', maxsplit=2))

Output:

输出:

['', 'B', 'C', 'D']['', 'B', 'CAD']

Notice that empty string is returned when the first character matches the separator.

请注意,当第一个字符与分隔符匹配时,将返回空字符串。

用户输入的CSV字符串拆分示例 (CSV String Split Example with User Input)

Finally, let’s look at a real-life example where the user will enter the CSV data and we will split it into the list of strings.

最后,让我们看一个真实的例子,用户将输入CSV数据并将其拆分为字符串列表。

input_csv = input('Please enter CSV Data\n')input_csv_split_list = input_csv.split(sep=',')print('Input Data Length =', len(input_csv_split_list))print('List of inputs =', input_csv_split_list)

Output:

输出:

Please enter CSV DataJava,Android,Python,iOS,jQueryInput Data Length = 5List of inputs = ['Java', 'Android', 'Python', 'iOS', 'jQuery']

That’s all for python string split() function examples. It’s a very useful function to split a string into the list based on some delimiter.

这就是python字符串split()函数示例的全部内容。 这是一个非常有用的功能,可以基于某些定界符将字符串拆分为列表。

Python字符串rsplit() (Python String rsplit())

Python string rsplit() function is very similar to split() function. The only difference is that the splits are done starting at the end of the string and working to the front.

Python字符串rsplit()函数与split()函数非常相似。 唯一的区别是分割从字符串的末尾开始一直到最前面。

Let’s look at some of the rsplit() function examples.

让我们看一些rsplit()函数示例。

# rsplit() examples = 'Python is Awesome'str_list = s.rsplit(sep=' ')print(str_list)str_list = s.rsplit(sep=' ', maxsplit=1)print(str_list)s = '  Java  Python iOS    Android  'str_list = s.rsplit()print(str_list)multiline_str = 'Hi There\nHow are you?\nI am fine'multiline_str_split_list = multiline_str.rsplit(sep='\n')for s in multiline_str_split_list:    print(s)s = 'Hi||Hello||Adios'str_list = s.rsplit('||')print(str_list)# using split() with str classprint(str.rsplit('ABACAD', sep='A'))print(str.rsplit('ABACAD', sep='A', maxsplit=2))# csv and user input exampleinput_csv = input('Please enter CSV Data\n')input_csv_split_list = input_csv.rsplit(sep=',')print('Input Data Length =', len(input_csv_split_list))print('List of inputs =', input_csv_split_list)

Output:

输出:

['Python', 'is', 'Awesome']['Python is', 'Awesome']['Java', 'Python', 'iOS', 'Android']Hi ThereHow are you?I am fine['Hi', 'Hello', 'Adios']['', 'B', 'C', 'D']['AB', 'C', 'D']Please enter CSV Datax,y,zInput Data Length = 3List of inputs = ['x', 'y', 'z']

Notice that the difference is visible when maxsplit argument is provided. Otherwise, the split() and rsplit() function output is same.

注意,当提供maxsplit参数时,差异是可见的。 否则,split()和rsplit()函数的输出相同。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://famzd.baihongyu.com/

你可能感兴趣的文章
UI基础--烟花动画
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>