Re: [Tutor] Question Regarding startswith()

2018-06-04 Thread Peter Otten
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 "", line 1, in 
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
>>> line = line.decode()
>>> line.startswith("1 ")
True


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question Regarding startswith()

2018-06-04 Thread Alan Gauld via Tutor
On 04/06/18 16:57, 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:

I have no idea what you are talking about and do not know what the SGP4
or io.py files are.
However...

>   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 depends on which startswith() method you are looking at.

If line is a string you are correct but... if line is a bytes
object then its startwith() method expects a bytes argument.

So I suspect that line is of type bytes(*) and you must convert
it to a string or use a bytes first argument.

(*)You can check by adding a

print("type of line: ", type(line))

in your code.


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Question Regarding startswith()

2018-06-04 Thread Jeremy Ogorzalek
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!

Jeremy Ogorzalek
M.S., Aerospace Engineering
Virginia Tech
443-812-3121
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor