16

I am attempting to pipe something to a subprocess using the following line:

p.communicate("insert into egg values ('egg');");

TypeError: must be bytes or buffer, not str

How can I convert the string to a buffer?

3 Answers 3

13

The correct answer is:

p.communicate(b"insert into egg values ('egg')")

Note the leading b, telling you that it's a string of bytes, not a string of unicode characters. Also, if you are reading this from a file:

value = open('thefile', 'rt').read()
p.communicate(value)

The change that to:

value = open('thefile', 'rb').read()
p.communicate(value)

Again, note the 'b'. Now if your value is a string you get from an API that only returns strings no matter what, then you need to encode it.

p.communicate(value.encode('latin-1'))

Latin-1, because unlike ASCII it supports all 256 bytes. But that said, having binary data in unicode is asking for trouble. It's better if you can make it binary from the start.

Sign up to request clarification or add additional context in comments.

Comments

9

You can convert it to bytes with encode method:

>>> "insert into egg values ('egg');".encode('ascii')    # ascii is just an example
b"insert into egg values ('egg');"

2 Comments

An extension to the answer: In Python 3 all strings are Unicode, and they probably need an encoding while transferring to the application so that app will understand. That's what the ascii is for.
@extraneon: Yes, all strings are unicode in Python 3. Which is why you don't use strings to hold data that is supposed to be transferred, you use bytes. Encoding is thus mostly unecessary, if you keep the data in the right format from the start.
0

Some people come here looking for how to convert string to buffer / stream / fileobject that can be read by say open(file) or read_csv , so contributing with a solution here:

csv_contents = """
col1,col2
valA,valB
"""

buffer = StringIO(csv_contents)

df = pd.read_csv(buffer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.