startswith(前缀[,start [,end]])查询 [英] startswith( prefix[, start[, end]]) Query

查看:251
本文介绍了startswith(前缀[,start [,end]])查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




startswith(前缀[,start [,end]])状态:


如果字符串以前缀开头,则返回True ,否则返回False。

前缀也可以是要查找的后缀元组。但是,当我尝试
并添加一个后缀元组时,我收到以下错误:


类型错误:预期字符缓冲区对象


例如:


file = f.readlines()

for line in file:

if line.startswith((" abc"," df"))

代码


会产生上述错误

为了克服这个问题,我目前只是加入个人

startwith方法

即if line.startswith(" if")或line.startswith( df)

但是知道必须有一种方法可以在一个元组中定义所有后缀。


提前谢谢

解决方案

cj *** @ bath.ac.uk 写道:





startswith(前缀[,start [,end]])状态:


如果字符串以前缀o开头,则返回True therwise返回False。

前缀也可以是要查找的后缀元组。



功能的特定方面(元组中的多个

前缀)仅添加了Python 2.5。如果你使用< = 2.4,那么你需要使用或或或者其他一些方法,

例如循环一系列前缀。


TJG


On 9月6日上午7:09,cj ... @ bath.ac.uk写道:





startwith (前缀[,start [,end]])状态:


如果字符串以前缀开头,则返回True,否则返回False。

前缀也可以是要寻找的后缀元组。但是,当我尝试
并添加一个后缀元组时,我收到以下错误:


类型错误:预期字符缓冲区对象


例如:


file = f.readlines()

for line in file:

if line.startswith((" abc"," df"))

代码


会产生上述错误


(剪断)


您看到使用旧版本的Python。

对我而言,它的工作方式与2.5.1,

但遇到了你用2.4.4描述的问题:


Python 2.5.1c1(r251c1:54692,2007年4月17日,21 :12:16)

[GCC 4.0.0(Apple Computer,Inc。build 5026)]在darwin上

输入help,copyright, ;学分"或许可证或欲获得更多信息。


>> line =" foobar"
if line.startswith ((" foo"," bar")):打印行



....

foobar


>> if line.startswith((" foo", " bar")):



....打印行

.... < br $>
foobar

VS.


Python 2.4.4(#1,2006年10月18日,10:34:39)
[GCC 4.0.1(Apple Computer,Inc。build 5341)]


>> line =" foobar"
if line.startswith ((" foo"," bar")):打印行



....

Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:预期字符缓冲区对象

-

希望这会有所帮助,

史蒂文


cj *** @ bath.ac.uk écrit:





startswith(前缀[,start [,end]])状态:


如果字符串以前缀开头,则返回True,否则返回错误。

前缀也可以是要查找的后缀元组。但是,当我尝试
并添加一个后缀元组时,我收到以下错误:


类型错误:预期字符缓冲区对象


例如:


file = f.readlines()

for line in file:



略贵,但是:

1 /你不应该使用''file''作为标识符,它暗示内置

文件类型

2 / FWIW,这对于一系列行也是一个非常糟糕的命名选择 - 为什么

不只是将这个列表命名为''lines''? - )

3 /无论如何,除非你需要将整个列表存储在内存中,否则你会更好地使用迭代器成语(Python文件是可迭代的):


f = open(''some_file.ext'')

for line in f:

print line


if line.startswith((" abc",df))

CODE


它会产生上述错误或



我建议您阅读相应版本的文档吗?那个

是,你安装的Python版本对应的那个? - )


将元组传递给str.startswith是2.5中的新功能。我打赌你在2.4或更老的版本上尝试它是b $ b。


为了解决这个问题,我目前只是加入个人

以方法开头

即如果line.startswith(if)或line.startswith(df)

但是知道那里必须是一种在一个元组中定义所有后缀的方法。



您可能想尝试使用正则表达式,但我不确定它是否值得(提示:

timeit模块非常适合快速的小型基准测试。


否则,您还可以编写自己的测试功能:


def str_starts_with(astring) ,*前缀):

startswith = astring.startswith

前缀中的前缀:

如果startswith(前缀):

返回true

返回false

for line in f:

if str_starts_with(line,''abc,' 'de'',''xxx''):

#CODE HERE


HTH


Hi

startswith( prefix[, start[, end]]) States:

Return True if string starts with the prefix, otherwise return False.
prefix can also be a tuple of suffixes to look for. However when I try
and add a tuple of suffixes I get the following error:

Type Error: expected a character buffer object

For example:

file = f.readlines()
for line in file:
if line.startswith(("abc","df"))
CODE

It would generate the above error

To overcome this problem, I am currently just joining individual
startswith methods
i.e. if line.startswith("if") or line.startswith("df")
but know there must be a way to define all my suffixes in one tuple.

Thanks in advance

解决方案

cj***@bath.ac.uk wrote:

Hi

startswith( prefix[, start[, end]]) States:

Return True if string starts with the prefix, otherwise return False.
prefix can also be a tuple of suffixes to look for.

That particular aspect of the functionality (the multiple
prefixes in a tuple) was only added Python 2.5. If you''re
using <= 2.4 you''ll need to use "or" or some other approach,
eg looping over a sequence of prefixes.

TJG


On Sep 6, 7:09 am, cj...@bath.ac.uk wrote:

Hi

startswith( prefix[, start[, end]]) States:

Return True if string starts with the prefix, otherwise return False.
prefix can also be a tuple of suffixes to look for. However when I try
and add a tuple of suffixes I get the following error:

Type Error: expected a character buffer object

For example:

file = f.readlines()
for line in file:
if line.startswith(("abc","df"))
CODE

It would generate the above error

(snipped)

You see to be using an older version of Python.
For me it works as advertised with 2.5.1,
but runs into the problem you described with 2.4.4:

Python 2.5.1c1 (r251c1:54692, Apr 17 2007, 21:12:16)
[GCC 4.0.0 (Apple Computer, Inc. build 5026)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>line = "foobar"
if line.startswith(("foo", "bar")): print line

....
foobar

>>if line.startswith(("foo", "bar")):

.... print line
....
foobar
VS.

Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>line = "foobar"
if line.startswith(("foo", "bar")): print line

....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: expected a character buffer object
--
Hope this helps,
Steven


cj***@bath.ac.uk a écrit :

Hi

startswith( prefix[, start[, end]]) States:

Return True if string starts with the prefix, otherwise return False.
prefix can also be a tuple of suffixes to look for. However when I try
and add a tuple of suffixes I get the following error:

Type Error: expected a character buffer object

For example:

file = f.readlines()
for line in file:

slightly OT, but:
1/ you should not use ''file'' as an identifier, it shadowas the builtin
file type
2/ FWIW, it''s also a pretty bad naming choice for a list of lines - why
not just name this list ''lines'' ?-)
3/ anyway, unless you need to store this whole list in memory, you''d be
better using the iterator idiom (Python files are iterables):

f = open(''some_file.ext'')
for line in f:
print line

if line.startswith(("abc","df"))
CODE

It would generate the above error

May I suggest that you read the appropriate version of the doc ? That
is, the one corresponding to your installed Python version ?-)

Passing a tuple to str.startswith is new in 2.5. I bet you''re trying it
on a 2.4 or older version.

To overcome this problem, I am currently just joining individual
startswith methods
i.e. if line.startswith("if") or line.startswith("df")
but know there must be a way to define all my suffixes in one tuple.

You may want to try with a regexp, but I''m not sure it''s worth it (hint:
the timeit module is great for quick small benchmarks).

Else, you could as well write your own testing function:

def str_starts_with(astring, *prefixes):
startswith = astring.startswith
for prefix in prefixes:
if startswith(prefix):
return true
return false

for line in f:
if str_starts_with(line, ''abc, ''de'', ''xxx''):
# CODE HERE

HTH


这篇关于startswith(前缀[,start [,end]])查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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