Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: a bytes-like object is required, not 'str' #3

Open
abrozzi opened this issue Jun 23, 2021 · 3 comments
Open

TypeError: a bytes-like object is required, not 'str' #3

abrozzi opened this issue Jun 23, 2021 · 3 comments

Comments

@abrozzi
Copy link

abrozzi commented Jun 23, 2021

Dear Ryan,

referred to this klebgenomics/Kaptive#15 (comment):

python SRST2-table-from-assemblies/srst2_table_from_assemblies.py 
--assemblies SRST2-table-from-assemblies/example_data/sample* 
--gene_db SRST2-table-from-assemblies/example_data/gene_db.mfasta 
--output test

gives me the following error:

Traceback (most recent call last):
 File "SRST2-table-from-assemblies/srst2_table_from_assemblies.py", line 335, in <module>
   main()
 File "SRST2-table-from-assemblies/srst2_table_from_assemblies.py", line 52, in main
   blast_results = blast_assembly(assembly, args.gene_db, args.algorithm, unique_allele_symbols)
 File "SRST2-table-from-assemblies/srst2_table_from_assemblies.py", line 164, in blast_assembly
   for line in blast_results_iterator(out):
 File "SRST2-table-from-assemblies/srst2_table_from_assemblies.py", line 255, in blast_results_iterator
   nextnl = results.find('\n', prevnl + 1)
TypeError: a bytes-like object is required, not 'str'

Thank you!
-A

@abrozzi
Copy link
Author

abrozzi commented Jun 24, 2021

Resolved: It works with python2.7.

It crashes with python3.8.

This is because here at line 255:

 http://stackoverflow.com/questions/3054604/iterate-over-the-lines-of-a-string
def blast_results_iterator(results):
    prevnl = -1
    while True:  
      nextnl = results.find('\n', prevnl + 1)
      if nextnl < 0: break
      yield results[prevnl + 1:nextnl]
      prevnl = nextnl

python2.7 and python3.8 treats differently bytes and string.

A possible solution to make it version independent would be to save somewhere the output of blastn and read it as tab separated values table.

my two cents,
Alex

@ronaldgevern
Copy link

The reason for this error is that in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes instead. We can convert bytes to string using bytes class decode() instance method, So you need to decode the bytes object to produce a string. In Python 3 , the default encoding is "utf-8" , so you can use directly:

b"python byte to string".decode("utf-8")

Python makes a clear distinction between bytes and strings . Bytes objects contain raw data — a sequence of octets — whereas strings are Unicode sequences . Conversion between these two types is explicit: you encode a string to get bytes, specifying an encoding (which defaults to UTF-8); and you decode bytes to get a string. Clients of these functions should be aware that such conversions may fail, and should consider how failures are handled.

@PaynDita
Copy link

PaynDita commented Jan 3, 2023

a bytes-like object is required not 'str'

simply add .decode()
eg;

If keyword in load:

return load

Error:

if keyword in load:

TypeError: a bytes-like object is required not 'str'

solution:

If keyword in load.decode():

return load

For easy understanding click the below link:
https://payndita.wixsite.com/it-garage/post/a-bytes-like-object-is-required-not-str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants