-1

below is a section of my code where i am trying to output a string and an integer to a file. It would not let me output more than on thing at a time so I had to put them on seperate lines. I also now have an error saying:

TypeError: Expected a character buffer object

referring to the line outputting the variable count. Can someone tell me how to fix this error? Also if I could somehow combine all this into one line that would be cool too. Thanks!

print outfile.write ("(" + currentuser + ")")
print outfile.write (" ")
print outfile.write (count)
4

2 回答 2

1

文件对象的.write()方法接受一个字符串参数。要写入整数,您需要先将其转换为字符串。

outfile.write("(%s) %s" % (currentuser, count))

可能是您正在寻找的。我不确定你为什么要print返回值,因为.write()不返回任何东西。

于 2012-05-24T06:48:02.310 回答
0

如果这是 Python(我不确定),请尝试

print outfile.write ("(" + currentuser + ") " + str(count))

或者

print outfile.write("(%s) %d" % (currentuser, count))

或者

print outfile.write("({0}) {1}".format(currentuser, count))
于 2012-05-24T06:47:54.070 回答