Asyncio decode utf-8 with StreamReader

前端 未结 1 1903
悲&欢浪女
悲&欢浪女 2021-02-07 22:43

I am getting used to asyncio and find the task handling quite nice, but it can be difficult to mix async libraries with traditional io libraries. The problem I am currently fac

1条回答
  •  独厮守ぢ
    2021-02-07 23:04

    You can use an IncrementalDecoder:

    Utf8Decoder = codecs.getincrementaldecoder('utf-8')
    

    With your example:

    decoder = Utf8Decoder(error='strict')
    while not stream.at_eof():
        data = await stream.read(4)
        print(decoder.decode(data), end='')
    

    Output:

    Mädchen mit Biß
    

    0 讨论(0)
提交回复
热议问题