python写入文本报错TypeError: expected a string or other character buffer object

今天用python写入文本,

 file_object2 = open('result.txt', 'w')
file_object2.write(bookid_list)
file_object2.close( )

报错 TypeError: expected a string or other character buffer object

原因:

是写入文件要求写入内容是str,直接转换成str即可,如下:

 file_object2 = open('result.txt', 'w')
file_object2.write(str(bookid_list))
file_object2.close()

补充:

还有一个读操作的代码:

 file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
上一篇:hdu 2665 划分树模板题(可作为模板)


下一篇:SU Demo之01MakingData--02MultiShot