Python 类型错误:应为字符缓冲区对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24439988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: expected a character buffer object
提问by
I am running into the following error while writing the value into a file. Can you please help me figure out what is the issue here and how to fix it?
将值写入文件时遇到以下错误。你能帮我弄清楚这里的问题是什么以及如何解决吗?
row = 649
with open(r'\loc\dev\Build_ver\build_ver.txt','r+') as f:
f.write(row)
print row
Error:
错误:
Traceback (most recent call last):
File "latest_rev.py", line 6, in <module>
f.write(row)
TypeError: expected a character buffer object
采纳答案by timgeb
Assuming you just want to write the string '649'
to the file, change row
to '649'
or issue f.write(str(row))
.
假设您只想将字符串写入'649'
文件,请更改row
为'649'
或发出f.write(str(row))
.
回答by Roozbeh Zabihollahi
I had the same error, in my code:
我有同样的错误,在我的代码中:
s.translate(table)
The s
obj was string
. The issue was s.translate
was expecting a unicode string. So, the fix was to use:
的s
OBJ是string
。问题是s.translate
期待一个 unicode 字符串。因此,修复方法是使用:
unicode(s).translate(table)
回答by bruh
You can do what timgeb did or you can do
你可以做 timgeb 所做的或者你可以做的
row = str(649)