Reputation: 157
I want to save the values from the function call in a txt file but I am getting the typeerror: expected a character buffer object
def printme( x ):
"Function f(x) = x(x+2)"
print x*(x+2)
return
myfile = open('xyz.txt', 'w')
for w in b:
myfile.write('%d' (printme(w)))
myfile.close()
Upvotes: 0
Views: 149
Reputation: 99
I think this is what you want
def printme( x ):
"Function f(x) = x(x+2)"
return x*(x+2)
b = [1,2,3,4,5]
myfile = open('xyz.txt', 'w')
for w in b:
myfile.write(str(printme(w)))
myfile.write("\n")
# !!! EDIT !!!
myfile.close()
Upvotes: 1
Reputation: 365657
There are at least four problems with your code as posted, and none of them will demonstrate the error you're actually seeing.
First, there's an IndentationError
:
myfile = open('xyz.txt', 'w')
for w in b:
myfile.write('%d' (printme(w)))
You can't randomly indent code wherever you want; the for
statement follows the assignment statement, and the myfile.write
call is supposed to be inside the for
loop. So:
myfile = open('xyz.txt', 'w')
for w in b:
myfile.write('%d' (printme(w)))
If you fix that, your next error will be a TypeError
, but it will be for 'str' object is not callable
, not anything about a character buffer object.
In Python, anything of the form foo (bar)
is a function call—an attempt to call foo
like a function, with bar
as its only argument. So, this expression:
'%d' (printme(w))
… attempts to call the string '%d'
as a function, passing the result of printme(w)
as its only argument. That's obviously not going to work, because strings aren't callable like functions. Hence the exception.
You probably wanted to use the %
operator there, like this:
'%d' % (printme(w))
That's close, but still not quite right, because (printme(w))
is not a tuple of one value. Tuples are created by commas, not by parentheses, so a tuple of one value has to have a comma in it. So:
'%d' % (printme(w),)
(You don't actually need a tuple here, because %
has special handling for single values. But if you're trying to create a tuple, you should get it right, not get it wrong and have your code possibly work anyway…)
And now we're getting close, but we've got another problem: printme
doesn't return a number, it prints a number, and then returns None
. (That's the default value you get from any function that doesn't return
something different.) So, the %
operator will give you yet another TypeError
, this time saying '%d" format: a number is required, not NoneType
.
To fix that, you want to return
the value instead of (or in addition to) printing it:
def printme( x ):
"Function f(x) = x(x+2)"
return x*(x+2)
Of course a function named printme
that instead returns a value is a bit misleading, so you might want to rename it at this point…
If you fix that, then everything works. There is no TypeError
saying expected a character buffer object
anywhere.
Most likely, in your actual code, you're trying to write
the value you got back from printme
, without formatting it into a string first (with that %
operator or otherwise). For example, if you do this:
myfile.write(printme(w))
… that's either trying to write None
(if you didn't fix the last problem) or some number (if you did), neither of which is a string (or other "character buffer object"), hence the error.
To fix that, just use the code you showed us instead of different code.
Putting it all together, here's working code that does what you're apparently trying to do:
def printme( x ):
"Function f(x) = x(x+2)"
return x*(x+2)
myfile = open('xyz.txt', 'w')
for w in b:
myfile.write('%d' % (printme(w),))
myfile.close()
Upvotes: 7