Bonson
Bonson

Reputation: 1468

replacing text in bytes data type in python

The lines below give the error: TypeError: 'str' does not support the buffer interface. I have searched a lot. This is an issue in Python 3.

with codecs.open(filename, 'w', 'utf-8') as o:
    text = "I charge it at night & morning."
    txtEncode = text.encode('utf8')
    data = txtEncode.replace("&", "&")
    o.write(data)

Kindly suggest.

Upvotes: 2

Views: 1686

Answers (1)

VPfB
VPfB

Reputation: 17322

The codec will do all necessary encoding work when saving data to the file. Thus you can forget about the encoding details and do your work conveniently on strings.

This means: don't do this:

txtEncode = text.encode('utf8')

because type of txtEncode is bytes.

Here is the corrected code:

with codecs.open(filename, 'w', 'utf-8') as o:
    text = "I charge it at night & morning."
    data = text.replace("&", "&")
    o.write(data)

For a simple case like yours, open may be used instead of codecs.open. From the Python3 docs:

While the builtin open() and the associated io module are the recommended approach for working with encoded text files, this module [i.e. codecs] provides additional utility functions and classes that allow the use of a wider range of codecs when working with binary files

Upvotes: 1

Related Questions