hoobastank
hoobastank

Reputation: 11

TypeError: 'str' does not support the buffer interface & configparser

I'm newbie here and I wouldn't want to ask such a easy question as my first post but I don't know anything about Python even I'm a PHP/C programmer.

I have a python script in Figway tools which is called RegisterDevice.py to register my own sensor hardware to FIWARE Lab. But some code lines of that script doesn't work as I expected because of Python3.4. This may not be my problem but I don't have too much time to wait an official solution that's why I thought that I could resolve it as a person who is familiar to the programming.

I've searched on the web for solution but I couldn't find any exact solution for it yet. As far as I read bytes and unicode strings are two different types in Python3.x but I couldn't realize where I have to encode or maybe decode string to other type on the code. Maybe I have to do something else...

Here is the part of script which gave me error like above.

# Load the configuration file
with open(CONFIG_FILE,'r+') as f:
    sample_config = f.read()
#config = ConfigParser.RawConfigParser(allow_no_value=True)
config = configparser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))        

Error:

Traceback (most recent call last):
  File "RegisterDevice.py", line 47, in <module>
    config.readfp(io.BytesIO(sample_config))
TypeError: 'str' does not support the buffer interface

Upvotes: 1

Views: 1360

Answers (2)

Peter
Peter

Reputation: 21

Firstly readfp() is deprecated in Python3 and you should use read_file().

The best way is probably using the read() function directly when you want to work with a file. You should set encoding as the second parameter if you expect non-ASCII characters inside the file.

The alternative is to read_string() and give it a string directly.

Upvotes: 2

Jessamyn Smith
Jessamyn Smith

Reputation: 1649

I have been doing work very similar to this, and I believe this script runs, but you will have to verify if it gives you the desired results:

import configparser

with open('.coveragerc','r+') as f:
#config = ConfigParser.RawConfigParser(allow_no_value=True)
    config = configparser.RawConfigParser(allow_no_value=True)
    config.readfp(f)

Upvotes: 0

Related Questions