Rumah > Soal Jawab > teks badan
dic = {2:'bob',3:'alice'}
output = open('/Users/Air/itchat/push.txt','wb')
for i in dic:
print (i,dic[i])
write_str = str(i) + ' ' + str(dic[i]) + '\n'
print(type(write_str))
output.write(write_str)
output.close()
PyCharm运行报错如下:
2 bob
<class 'str'>
Traceback (panggilan terbaharu terakhir):
Fail "/Users/Air/itchat/test.py", baris 8, dalam <modul>
output.write(write_str)
TypeError: objek seperti bait diperlukan, bukan 'str'
Proses selesai dengan kod keluar 1
阿神2017-06-30 09:57:31
用wb打开,需要encode
`dic = {2:'bob',3:'alice'}
output = open('/Users/Air/itchat/push.txt','wb')
untuk saya dalam dic:
print (i,dic[i])
write_str = str(i) + ' ' + str(dic[i]) + '\n'
print(type(write_str))
output.write(write_str.encode())
output.close()`