TypeError: 'str' does not support the buffer interface
If you use Python3x then string
is not the same type as for Python 2.x, you must cast it to bytes (encode it).
plaintext = input("Please enter the text you want to compress")
filename = input("Please enter the desired filename")
with gzip.open(filename + ".gz", "wb") as outfile:
outfile.write(bytes(plaintext, 'UTF-8'))
Also do not use variable names like string
or file
while those are names of module or function.
EDIT @Tom
Yes, non-ASCII text is also compressed/decompressed. I use Polish letters with UTF-8 encoding:
plaintext = 'Polish text: ąćęłńóśźżĄĆĘŁŃÓŚŹŻ'
filename = 'foo.gz'
with gzip.open(filename, 'wb') as outfile:
outfile.write(bytes(plaintext, 'UTF-8'))
with gzip.open(filename, 'r') as infile:
outfile_content = infile.read().decode('UTF-8')
print(outfile_content)
TypeError: 'str' does not support the buffer interface in python
You're running a python 2 script with python 3. Python 3 now returns bytes
no longer str
when reading from a binary stream.
3 choices:
run it with python 2. That if you don't have the rights/time to adapt the script, not recommended as python 3 is becoming more and more the norm.
change your code to insert a
decode
function (it will continue to work in python 2):
username = cred_file.readlines()[0].decode().split(';')[0]
If file is opened in read/binary mode, readlines
returns a list of bytes
not str
. You have do decode the bytes
into a str
to apply str methods.
- open the file in "r" instead of "rb".
readlines
then returns a list of str and your code will work. Sometimes it can be problematic on windows because of need to preserve the carriage return (\r
) chars, so look out for side effects in your code.
Note: cred_file.readlines()[0]
is a questionable construction: you're reading the whole file lines, and drop all the lines but the first. Not very efficient I/O and CPU wise.
Prefer that: cred_file.readline()
which is equivalent to read the first line.
If you need to read all the lines for further processing, then store the result of readlines
in a list.
TypeError: str does not support buffer interface
In python 3, bytes strings and unicode strings are now two different types.
Since sockets are not aware of string encodings, they are using raw bytes strings, that have a slightly different interface from unicode strings.
So, now, whenever you have a unicode string that you need to use as a byte string, you need to encode()
it. And when you have a byte string, you need to decode
it to use it as a regular (python 2.x) string.
Unicode strings are quotes enclosed strings.
Bytes strings are b""
enclosed strings
See What's new in python 3.0 .
TypeError: 'str' does not support the buffer interface
You're using Python 3, where there is a strict division between text (str
) and data (bytes
). Text can't be written to a file if you don't explicitly encode it first.
There are two ways to do this:
1) Open the file in text mode (possibly with an encoding specified) so that strings are automatically encoded for you:
with open("lt.txt", 'at', encoding='utf8') as outfile:
outfile.write(hash_digest + '\n') # or print(hash_digest, file=outfile)
If you don't specify the encoding yourself when opening the file in text mode, the default encoding of your system locale would be used.
2) Encode the strings manually like you tried. But don't try to mix str
with bytes
like you did, either use a byte literal:
hash_digest = hash_digest.encode('utf-8')
with open("lt.txt", 'ab') as outfile:
outfile.write(hash_digest + b'\n') # note the b for bytes
or encode after adding the newline:
outfile.write((hash_digest + '\n').encode('utf-8'))
Python: 'str' does not support the buffer interface on subprocess.communicate (migrating to 3.x)
In python 3, subprocess
streams are binary.
To write a string, just encode a binary, in your case ascii
codec is OK:
fdisk_cmd.communicate(cmd_a.encode("ascii"))
TypeError : 'str' does not support the buffer interface
Just guessing since you didn't show the stack trace, or even the line that generates the error. But I think it can be fixed:
s.write((l + '\n').encode("utf8"))
Related Topics
Python Pandas: How to Specify Data Types When Reading an Excel File
Imports in _Init_.Py and 'Import As' Statement
Underscore VS Double Underscore with Variables and Methods
How to Check Blas/Lapack Linkage in Numpy and Scipy
Compulsory Usage of If _Name_=="_Main_" in Windows While Using Multiprocessing
Comparing Python Dictionaries and Nested Dictionaries
How to Use Inspect to Get the Caller's Info from Callee in Python
Differencebetween Encode/Decode
How to Access Pandas Groupby Dataframe by Key
How to Set the Absolute Position of Figure Windows with Matplotlib
Django Submit Two Different Forms with One Submit Button
Why Do "Not a Number" Values Equal True When Cast as Boolean in Python/Numpy
Easy Pretty Printing of Floats
Differencebetween Root.Destroy() and Root.Quit()
Django Return Redirect() with Parameters