split函数python_奇技淫巧 - Python分割字符串的5个示例

在这个Python教程中,我们将学习Python split字符串函数。与len不同,有些函数是特定于字符串的。要使用字符串函数,输入字符串的名称、dot、函数的名称和函数需要的 所有参数:string.function(arguments)。可以使用内置的string split函数根据分隔符将字符串分解为一组更小的字符串。

Python string.split 语法

使用string.split的语法如下:

string.split([separator[, maxsplit]])

说明:separator 是分隔符字符串

如果指定了maxsplit,则最多完成maxsplit分割(因此,列表最多包含maxsplit + 1个元素)

如果没有指定maxsplit或-1,那么拆分的数量就没有限制(所有可能的拆分都进行了)。

如果 separator 指定,则连续的分隔符不会分组在一起,并被视为定界空字符串(例如, '1,,2'.split(',') 返回 ['1', '', '2'] )

如果 separator 未指定或为None,则连续的 空白 行将被视为单个分隔符,并且如果字符串具有前导或尾随空格,则结果在开头或结尾将不包含空字符串。例如, ' 1 2 3 '.split 返回 ['1', '2', '3']

示例1:使用空格分割字符串

在这个示例脚本中,我们将使用空格作为分隔符将包含字符串的句子分割成多个子字符串。如果没有要定义的分隔符,那么可以只提供split,它在默认情况下将分隔符视为None。

#!/usr/bin/env python3

mystring = "This is Python Tutorial linuxmi"

print(type(mystring)) ## This will return type as string

newstring = mystring.split ## split the string and store into newstring var

print(newstring) ## print the content of newstring

print(type(newstring)) ## the new type would be list after splitting

该脚本的输出:

['This', 'is', 'Python', 'Tutorial', 'linuxmi']

160609584056932224.PNG

string.split将在传递的参数上拆分和分割字符串,并返回列表中的所有部分。该列表将不包括分割字符。

示例2:使用逗号作为分隔符

在这个例子中,我们将定义一个分隔符逗号( , ),并将字符串分割成列表

#!/usr/bin/env python3

mystring = "abc,def,linuxmi"

print(type(mystring)) ## This will return type as string

newstring = mystring.split( ',') ## split the string using ',' and store into newstring var

print(newstring) ## print the content of newstring

print(type(newstring)) ## the new type would be list after splitting

该脚本的输出:

['abc', 'def', 'linuxmi']

160609584052516740.PNG

所以这次使用逗号分隔输出,因为我们使用了string.split(,)。类似地,您可以使用任何其他字符来分割字符串。

示例3:定义最大分割限制

默认情况下,如果您没有指定分割限制,那么所有可能的值将从提供的字符串中分割。在这个例子中,我们将maxlimit定义为1,这样在第一次拆分之后,Python将忽略其余的分隔符。

#!/usr/bin/env python3

mystring = "abc,def,ghi,tre,linuxmi.com"

print(type(mystring)) ## This will return type as string

## split the string using sep=',' with maxlimit=1 and store into newstring var

newstring = mystring.split( ',', 1)

print(newstring) ## print the content of newstring

print(type(newstring)) ## the new type would be list after splitting

该脚本的输出:

['abc', 'def,ghi,tre,linuxmi.com']

160609584056797422.PNG

从输出中可以看到,我们的字符串被分为两部分,在第一个分隔符匹配之后,所有其他逗号都将被忽略。

示例4:计算文件中单词的出现次数

该split方法将字符串在找到空格的任何地方分成几部分,并将字符串的所有部分存储在列表中。结果是字符串中的单词列表,尽管某些单词可能还会出现标点符号。

我们将使用split来计算“/usr/share/doc/grep/README”文件中的单词数量。你可以忽略try和except块,如果你还不熟悉它,可以专注于我正在执行实际任务的else块:

#!/usr/bin/env python3

filename = '/usr/share/doc/grep/README'

try:

withopen(filename, encoding= 'utf-8') asf:

contents= f.read

exceptFileNotFoundError:

print(f 'Sorry, the file {filename} does not exits')

else:

words = contents.split

num_words = len(words)

print(f 'The file {filename} has about {num_words} words.')

该脚本的输出:

The file /usr/share/doc/grep/README has about 372 words.

让我们用wc验证输出:

linuxmi@linuxmi:~/www.linuxmi.com$ wc -w /usr/share/doc/grep/README

372 /usr/share/doc/grep/README

因此,我们的脚本和wc的输出是相同的,这意味着split成功地分隔了单词。

160609584045482358.PNG

示例5:使用一个带有for循环的line分割字符串

在这个例子中,我们将使用一个line代码来分割字符串并打印超过5个字符的单词。

#!/usr/bin/env python3

mystring = 'linuxmi.com we are testing python split strin'

## One-Liner

w = [[x forx inline.split iflen(x)> 5] forline inmystring.split( 'n')]

## Result

print(w)

说明内部列表理解表达式

x forx inline.split iflen(x)> 5

使用字符串split函数将给定的行划分为单词序列。我们遍历所有单词x,如果它们的字符数超过5个,则将它们添加到列表中。

外部列表理解表达式创建上一条语句中使用的字符串行。再次,它使用split函数mystring在换行符上分割'n'。

该脚本的输出:

[['linuxmi.com', 'testing', 'python']]

160609584059137253.PNG

结论

在本教程中,我们学习了如何string.split使用不同的示例。我们可以将split与regex结合使用以添加更强大的功能,这些功能将在以后的教程中介绍。在这里,涵盖了在不同情况下将其与字符串一起使用的5个示例。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值