[Tutor] Question Regarding startswith()

Peter Otten __peter__ at web.de
Mon Jun 4 15:56:24 EDT 2018


Jeremy Ogorzalek wrote:

> Not sure this is how this is done, but here goes.
> 
> When I try to run the code in the SGP4 module, I get the following error,
> and it originates in the io.py script:
> 
> 
>   File "C:\ProgramData\Anaconda3\lib\site-packages\sgp4\io.py", line 131,
> in twoline2rv
>     assert line.startswith('1 ')
> 
> TypeError: startswith first arg must be bytes or a tuple of bytes, not str
> 
> But as far as I can tell in the documentation online (multiple sources)
> the first argument of startswith() is indeed supposed to be a string! It
> should be the string of text you are searching for! So, what gives? I've
> also tried to make the code happy by converting the string to bytes,
> tuple, etc.... and still get the same error. Any help would be much
> appreciated. Thanks!

Do you read the first two arguments of the twoline2rv() function from a 
file? If so you probably open that file binary mode, but you have to open it 
in text mode to get strings. If that's not possible because the file 
contains other binary data you may convert the byte strings to unicode 
explicitly:

>>> line = b"1 some bytes"
>>> line.startswith("1 ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
>>> line = line.decode()
>>> line.startswith("1 ")
True




More information about the Tutor mailing list