TypeError:执行open()时需要字符缓冲区对象 [英] TypeError: expected a character buffer object when doing open()

查看:74
本文介绍了TypeError:执行open()时需要字符缓冲区对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在学习Python。我在用数组和open()做简单的事情,有时这段代码行得通,有时却行不通!

So I'm learning Python. I was doing a simple thing with arrays and open(), and sometimes this code works, and sometimes it doesn't! Please help!

    print('Load? (Y/N)')
load = raw_input()
if load == "y":
    fin = open("myArr.bat", "r")
    myArr = fin.readline()
if load == "n":
    myArr = [0, 0, 0,
             0, 0, 0,
             0, 0, 0]
if load != "y" and load != "n":
    print 'WUT?'
    exit()

print (myArr[0]) ,  '|' ,  (myArr[1]) ,  '|' ,  (myArr [2])
print '----------'
print (myArr[3]) ,  '|' ,  (myArr[4]) ,  '|' ,  (myArr [5])
print '----------'
print (myArr[6]) ,  '|' ,  (myArr[7]) ,  '|' ,  (myArr [8])
print '_______________________________________________'
print 'What shall I change?'
print 'Number in array: '
foo = raw_input()
doo = int(float(foo))
print 'Number to change to: '
bar = raw_input()
dar = int(float(bar))
myArr[doo] = dar
print '_______________________________________________'
print (myArr[0]) ,  '|' ,  (myArr[1]) ,  '|' ,  (myArr [2])
print '----------'
print (myArr[3]) ,  '|' ,  (myArr[4]) ,  '|' ,  (myArr [5])
print '----------'
print (myArr[6]) ,  '|' ,  (myArr[7]) ,  '|' ,  (myArr [8])
fout = open("myArr.bat", "w")
fout.write(myArr)
fout.close()

它给了我这个:

   Traceback (most recent call last):
  File "Screen.py", line 35, in <module>
    fout.write(myArr)
TypeError: expected a character buffer object

推荐答案

这是因为 write 方法期望将字符串作为第一个参数,但是您正在将其传递给数组。

That's because the write method expects a string as the first argument, but you're passing it an array.

这篇关于TypeError:执行open()时需要字符缓冲区对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆