split()和string.whitespace [英] split() and string.whitespace

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

问题描述

我无法弄清楚为什么前两个语句可以正常工作,因为我希望他们能够接受这两个语句,而接下来的两个语句则没有。也就是说,前两个将

句子吐成其组成单词,而后两个单词则返回

整句句子。


导入字符串

来自字符串导入空白

mytext ="快速的棕色狐狸跳过懒狗。\ n"


print mytext.split()

print mytext.split('''')

print mytext.split(whitespace)

print string.split(mytext,sep = whitespace)

I am unable to figure out why the first two statements work as I
expect them to and the next two do not. Namely, the first two spit the
sentence into its component words, while the latter two return the
whole sentence entact.

import string
from string import whitespace
mytext = "The quick brown fox jumped over the lazy dog.\n"

print mytext.split()
print mytext.split('' '')
print mytext.split(whitespace)
print string.split(mytext, sep=whitespace)

推荐答案

2008年10月31日星期五11:53:30 -0700,Chaim Krause写道:
On Fri, 31 Oct 2008 11:53:30 -0700, Chaim Krause wrote:

我无法弄清楚为什么前两个语句按预期工作

他们和接下来的两个语句没有。也就是说,前两个将句子吐出

到其组成单词中,而后两个则返回整个句子

entact。


导入字符串

来自字符串导入空白

mytext ="快速的棕色狐狸跳过懒狗。\ n"


print mytext.split()

print mytext.split('''')
I am unable to figure out why the first two statements work as I expect
them to and the next two do not. Namely, the first two spit the sentence
into its component words, while the latter two return the whole sentence
entact.

import string
from string import whitespace
mytext = "The quick brown fox jumped over the lazy dog.\n"

print mytext.split()
print mytext.split('' '')



这会拆分字符串'' ''。

This splits at the string '' ''.


print mytext.split(whitespace)
print mytext.split(whitespace)



这个字符串拆分为''\\ \\ t \\\\\\\\\\\\\\\\\\\\\\\\\\参数是一个字符串而不是一组字符。

This splits at the string ''\t\n\x0b\x0c\r '' which doesn''t occur in
`mytext`. The argument is a string not a set of characters.


print string.split(mytext,sep = whitespace)
print string.split(mytext, sep=whitespace)



同样在这里。


Ciao,

Marc''BlackJack''Rintsch


Same here.

Ciao,
Marc ''BlackJack'' Rintsch


我无法弄清楚为什么前两个语句的工作原理为
I am unable to figure out why the first two statements work as I

期望它们和接下来的两个语句没有。也就是说,前两个将

句子吐成其组成单词,而后两个单词则返回

整句句子。


导入字符串

来自字符串导入空白

mytext ="快速的棕色狐狸跳过懒狗。\ n"


print mytext.split()

print mytext.split('''')

print mytext.split(whitespace)

print string.split(mytext,sep = whitespace)
expect them to and the next two do not. Namely, the first two spit the
sentence into its component words, while the latter two return the
whole sentence entact.

import string
from string import whitespace
mytext = "The quick brown fox jumped over the lazy dog.\n"

print mytext.split()
print mytext.split('' '')
print mytext.split(whitespace)
print string.split(mytext, sep=whitespace)



Split对文字字符串起作用,或者如果分隔符不是

指定的话,在一组数据上,拆分任意空格。


例如,尝试


s =" abcdefgbcdefgh"

s.split(" c")#[''ab'',''defgb'',''defgh'']

s.split(" fgb") #[''abcde'',''cdefgh'']

string.whitespace是一个字符串,所以split()尝试使用拆分

the literal空白,而不是一组空格。


-tkc


Split does its work on literal strings, or if a separator is not
specified, on a set of data, splits on arbitrary whitespace.

For an example, try

s = "abcdefgbcdefgh"
s.split("c") # [''ab'', ''defgb'', ''defgh'']
s.split("fgb") # [''abcde'', ''cdefgh'']
string.whitespace is a string, so split() tries to use split on
the literal whitespace, not a set of whitespace.

-tkc


2008年10月31日星期五:53 AM,Chaim Krause< ch *** @ chaim.comwrote:
On Fri, Oct 31, 2008 at 11:53 AM, Chaim Krause <ch***@chaim.comwrote:

我无法弄清楚为什么前两个语句可以正常工作>
期望他们和接下来的两个不会。也就是说,前两个将

句子吐成其组成单词,而后两个单词则返回

整句句子。


导入字符串

来自字符串导入空白

mytext ="快速的棕色狐狸跳过懒狗。\ n"


print mytext.split()

print mytext.split('''')

print mytext.split(whitespace)

print string.split(mytext,sep = whitespace)
I am unable to figure out why the first two statements work as I
expect them to and the next two do not. Namely, the first two spit the
sentence into its component words, while the latter two return the
whole sentence entact.

import string
from string import whitespace
mytext = "The quick brown fox jumped over the lazy dog.\n"

print mytext.split()
print mytext.split('' '')
print mytext.split(whitespace)
print string.split(mytext, sep=whitespace)



另请注意,没有参数的普通''mytext.split()'将分开
在你想要的任何空白角色上



干杯,

克里斯

- -

沿着鬣蜥的路径......
http:// rebertia.com


-
http://mail.python.org/mailman/listinfo/python-list


这篇关于split()和string.whitespace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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