startswith( prefix[, start[, end]]) Query

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Thu Sep 6 10:22:51 EDT 2007


On Sep 6, 7:09 am, cj... at 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




More information about the Python-list mailing list