4

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()

Its giving me this :

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

Please help!

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

2 Comments

Well how to I pass as a string, and how would I then later retrieve it? ty for thy help!
@PlazmotechBinary, if you want to store the serialized data from the array in a file, then later read the file and unserialize it, use pickle.
9

I'm going to guess that you get this error when you test your code and input 'n', but when you input 'y', it works just fine. This is because of these lines:

if load == "n":
myArr = [0, 0, 0,
         0, 0, 0,
         0, 0, 0]

This makes myArr a list. One does not simply write a list to a file. You must convert it into a string first (only strings can be written to files).

So depending on how you want to store this list in your file, you could do this:

fout = open("myArr.bat", "w")
fout.write(' '.join(map(str, myArr)))
fout.close()

This would essentially write the following line to myArr.bat (assuming myArr = [0, 0, 0, 0, 0, 0, 0, 0, 0]):

0 0 0 0 0 0 0 0 0

Hope this helps

2 Comments

Hmm... well, how would I then retrieve it? The only way I could think of would be: fin = open("myArr.bat", "r") myArr[0] = fin.readline(1) But that wouldn't work... because you have those spaces. But If I remove the spaces then you would have problems with 2 digit numbers... how can I do this?
myArr = map(int, open("myArr.bat").readline().strip().split()) If you don't specify "r" in open, "r" is used as the default mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.