Python split() 函数是如何工作的 [英] How does Python split() function works

查看:39
本文介绍了Python split() 函数是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def loadTest(filename): 
    f=open(filename,'r')
    k=0
    line=f.readline()
    labels=[]
    vectors=[]
    while line and k<4:
        k=k+1
        l=line[:-1].split(r'","')
        s=float(l[0][1:])
        tweet=l[5][:-1]
        print(l)
        line=f.readline()
     f.close()
     return

split(r'","') 在 python split 方法中实际上做了什么?

What do split(r'","') actually does inside python split method?

推荐答案

Raw string vs Python string

r'","'

r 是为了表明它是一个原始字符串.

原始字符串与常规 python 字符串有何不同?

How is a raw string different to a regular python string?

特殊字符原始字符串中丢失了它们的特殊含义.例如 \n 是 python 字符串中的换行符,它将在原始字符串中失去其含义,仅表示反斜杠后跟 n.

The special characters lose their special meaning inside a raw string. For example \n is a newline character inside a python string which will lose its meaning in a raw string and will simply mean backslash followed by n.

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

string.split() will break and split the string on the argument that is passed and return all the parts in a list. The list will not include the splitting character(s).

string.split('","') 将在每个 "," 上断开和拆分字符串并返回所有断开的列表中不包括 ","

string.split('","') will break and split the string on every "," and return all the broken parts in a list excluding ","

例如:

print 'Hello world","there you are'.split(r'","')

输出:

['Hello world', 'there you are']

<小时><小时><小时>

split() 可以做更多...

您可以通过传入一个额外的参数来指定您希望字符串分成多少部分.




split() can do even more...

You can specify how many parts you want your string to break into by passing in an extra parameter.

让我们考虑这个字符串:'hello,world,there,you,are'

Lets consider this string: 'hello,world,there,you,are'

  1. 拆分所有逗号并分成 n+1 个部分,其中 n 是逗号的数量:

>>>print 'hello,world,there,you,are'.split(',')
['hello', 'world', 'there', 'you', 'are']

  1. 在第一个逗号处拆分并仅分成 2 部分.

>>>'hello,world,there,you,are'.split(',',1)  
['hello', 'world,there,you,are']

  1. 将第一个和第二个逗号分开并分成 3 个部分.等等……

>>>'hello,world,there,you,are'.split(',',2)
['hello', 'world', 'there,you,are']

<小时>

还有更多...

来自文档:

如果拆分字符,即分隔符未指定或为无,则应用不同的拆分算法:连续空格的运行被视为单个分隔符,并且结果将在开始或结束时不包含空字符串,如果字符串有前导或尾随空格.因此,拆分空字符串或仅由空格组成的字符串并使用 None 分隔符将返回 [].

If splitting character(s) i.e separator is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

例如

>>>' 1  2   3  '.split()
['1', '2', '3']

>>>'  1  2   3  '.split(None, 1)
['1', '2   3  ']

>>>''.split()
[]

>>>'    '.split()
[]


>>>'      '.split(None)
[]

<小时>

甚至...



.

.

.


And even...



.

.

.

您还在寻找更多,这还不够吗?不要那么贪心:P.问问自己?,它会让你变得不贪心 :D (如果你知道正则表达式,你就会得到这个笑话)

Isn't it enough that you are looking for more? Don't be so greedy :P. Just question yourself?, it will make you non-greedy :D (You will get the joke if you know regular expressions)

这篇关于Python split() 函数是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆