skip to Main Content

"TypeError: can’t concat str to bytes" pops out at read(1) statement in following code.

def __init__(self, infile):
        self.infile = infile
        while self.infile.read(1) != 'T':
            pass
        else:
            .

infile is codecs.StreamReaderWriter object.

My system is Linux Debian, with python 3.11.4.

I am checking docs.python.org and other sites, but I have no idea where byte comes from.

I have changed infile to codecs.StreamReader object, but same error.

Any input will be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Not quite an answer, but one step ahead. I tried to extract the simplest sample.

    Following code works

    import codecs
    fse = codecs.open("/path/to/text/file", "r", "utf_8")
    fse.read(1)
    

    While following code chokes, as the title reads

    import codecs, io
    codecinfo = codecs.lookup("utf_8")
    fce = codecs.StreamReaderWriter(io.StringIO("bait"), codecinfo.streamreader, codecinfo.streamwriter)
    fce.read(1)
    

    Both of fce and fse are StreamReaderWriter object, one from file, the other from memory.

    Any suggestion will be appreciated.


  2. When parsing your data you’re getting a StreamReaderWriter with binary data ([see docs here][1]) so either you’ll want to convert your check string to bytes eg.

    while self.infile.read(1) != b'T':
    # do stuff
    

    notice the "b" inserted before your string – otherwise decode your byte type data eg.

    while self.infile.read(1).decode("utf-8") != 'T':
    # do stuff
    

    This is assuming the encoding of your data is utf-8 but you may need to verify that is an accurate assumption.
    [1]: https://docs.python.org/3/library/codecs.html#codecs.open

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search