string.split和“" .split之间的小不一致 [英] Small inconsistency between string.split and "".split

查看:100
本文介绍了string.split和“" .split之间的小不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

虽然写了一个小程序来帮助clpy的其他海报,我发现

a处理关键字参数之间的小不一致

string.split()和字符串的split()方法。我想知道是否有人曾经曾经偶然发现过它,并且如果有充分的理由让b $ b工作就好了。


两种实现都有两个参数:分隔符和

最大分割数(maxsplit)。但是,string.split()接受

maxsplit作为关键字参数,而mystring.split()则不然。在我的

的情况下,这意味着我不得不在我的例子中使用string.split(),

以避免必须处理分隔符。 />

**顺便说一句,我不得不避免与分隔符处理另一个令人讨厌的原因

原因:我认为我可以这样做:


mystring.split(string.whitespace,2)


保留默认的空白检测行为。但它不会以这种方式工作,因为没有实现split()。


----

Carlos Ribeiro

Consultoria em Projetos

博客: http: //rascunhosrotos.blogspot.com

博客: http:/ /pythonnotes.blogspot.com

邮件: ca ******* *@gmail.com

邮件: ca ******** @ yahoo.com

Hi all,

While writing a small program to help other poster at c.l.py, I found
a small inconsistency between the handling of keyword parameters of
string.split() and the split() method of strings. I wonder if someone
else had ever stumbled on it before, and if it has a good reason to
work like it is.

Both implementations take two parameters: the separator character and
the max number of splits (maxsplit). However, string.split() accept
maxsplit as a keyword parameter, while mystring.split() doesn''t. In my
case, it meant that I had to resort to string.split() in my example,
in order to avoid having to deal with the separator.

** BTW, I had to avoid dealing with the separator for another annoying
reason: I thought that I could do something like this:

mystring.split(string.whitespace, 2)

to preserve the default whitespace detecting behavior. But it won''t
work this way with neither implementation of split().

----
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmail.com
mail: ca********@yahoo.com

推荐答案

Carlos Ribeiro写道:
Carlos Ribeiro wrote:
写一个小程序为了帮助clpy的其他海报,我发现
string.split()的关键字参数和字符串的split()方法之间存在小的不一致。我想知道是否有其他人曾经偶然发现它,如果它有充分理由像它一样工作。

两个实现都有两个参数:分隔符和最大分裂数(maxsplit)。但是,string.split()接受
maxsplit作为关键字参数,而mystring.split()则不然。在我的
情况下,这意味着我必须在我的例子中使用string.split(),
以避免必须处理分隔符。


在这里工作:


c:\> python

Python 2.3.4(#53,May 25 2004,21:17:02)[MSC v.1200 32 bit(Intel)] on

win32

输入help,copyright, ;学分"或许可证或更多信息。
While writing a small program to help other poster at c.l.py, I found
a small inconsistency between the handling of keyword parameters of
string.split() and the split() method of strings. I wonder if someone
else had ever stumbled on it before, and if it has a good reason to
work like it is.

Both implementations take two parameters: the separator character and
the max number of splits (maxsplit). However, string.split() accept
maxsplit as a keyword parameter, while mystring.split() doesn''t. In my
case, it meant that I had to resort to string.split() in my example,
in order to avoid having to deal with the separator.
Works here:

c:\>python
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
s =''这是我的字符串''
s.split()
[''this'',''是'',''my'',''string''] s.split(''s'')
[''thi'',''i '',''我'',''tring''] s.split('s'',1)
[''thi'',''是我的字符串''] s.split( ''s'',2)
[''''''''''',''我的字符串'']

**顺便说一句,我不得不避免处理另一个令人讨厌的原因的分隔符:我以为我可以这样做:

mystring.split(string.whitespace,2)

保留默认的空白检测行为。但它不会以这种方式工作,既没有实现split()。
s = ''this is my string''
s.split() [''this'', ''is'', ''my'', ''string''] s.split(''s'') [''thi'', '' i'', '' my '', ''tring''] s.split(''s'', 1) [''thi'', '' is my string''] s.split(''s'', 2) [''thi'', '' i'', '' my string'']
** BTW, I had to avoid dealing with the separator for another annoying
reason: I thought that I could do something like this:

mystring.split(string.whitespace, 2)

to preserve the default whitespace detecting behavior. But it won''t
work this way with neither implementation of split().




我觉得这样可行:

s.split(无,2)
[''this'',''是'',''我的字符串''] s.split(无,1)



I think this works though:
s.split(None, 2) [''this'', ''is'', ''my string''] s.split(None, 1)



[''this'',''是我的字符串'']


-Peter


[''this'', ''is my string'']

-Peter


2004年9月13日星期一13:09:26 -0400,Peter Hansen< pe *** @ engcorp.com>写道:
On Mon, 13 Sep 2004 13:09:26 -0400, Peter Hansen <pe***@engcorp.com> wrote:
在这里工作:
< snip>
Works here:
<snip>
>>> s.split('s'',1)[''thi'',''是我的字符串'']>>> s.split('s'',2)
>>> s.split(''s'', 1) [''thi'', '' is my string''] >>> s.split(''s'', 2)




不幸的是,这不是*我想要求的。我是什么

说的是:


导入字符串

strings.split(maxsplit = 1)


有效,而


mystring.split(maxsplit = 1)


不行。简而言之,在strings.split()函数中,内置字符串方法不接受关键字

参数。唉,没有记录无

技巧 - 如果不了解它,我就没有其他方式可以获得


-

Carlos Ribeiro

Consultoria em Projetos

博客: http://rascunhosrotos.blogspot.com

博客: http://pythonnotes.blogspot.com

mail: ca ******** @ gmail.com

mail: ca ******** @ yahoo.com



Unfortunately, this is *not* what I had meant to ask for. What I am
saying is that:

import strings
strings.split(maxsplit=1)

works, while

mystring.split(maxsplit=1)

doesn''t. In short, the builtin string method doesn''t accept keyword
parameters while the strings.split() function does. Alas, the "None"
trick is not documented -- and without knowing about it, I had no
other way around.
--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmail.com
mail: ca********@yahoo.com


2004年9月13日星期一下午02:41:33 -0300, Carlos Ribeiro写道:

....
On Mon, Sep 13, 2004 at 02:41:33PM -0300, Carlos Ribeiro wrote:
....
...唉,没有记录无
技巧 - 并且不知道它,我没有其他方法。
... Alas, the "None"
trick is not documented -- and without knowing about it, I had no
other way around.




在2.3.4 Python库参考部分2.3.6.1字符串方法中,


""

split([sep [,maxsplit]])


返回一个列表字符串中的单词,使用sep作为

分隔符字符串。如果给出maxsplit,最多maxsplit

分割完成。如果未指定sep或None,则任何

空白字符串都是分隔符。

"""


I认为无这里记录了诀窍,因为字符串方法是

介绍。


-Inyeol



In 2.3.4 Python Library Reference section 2.3.6.1 String Methods,

"""
split([sep [,maxsplit]])

Return a list of the words in the string, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or None, any
whitespace string is a separator.
"""

I think "None" trick was documented here since string method was
introduced.

-Inyeol


这篇关于string.split和“&quot; .split之间的小不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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