小编Car*_*Kay的帖子

如何将数组写入文件,然后调用该文件并向数组添加更多内容?

因此,正如标题所暗示的那样,我正在尝试将数组写入文件,但随后我需要调用该数组并向其追加更多内容,然后将其写回同一个文件,然后一遍又一遍地执行相同的过程。

我到目前为止的代码是:

c = open(r"board.txt", "r")

current_position = []

if filesize > 4:
    current_position = [c.read()]
    print(current_position)
    stockfish.set_position(current_position)
    
else:
    stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")

#There is a lot more code here that appends stuff to the array but I don't want to #add anything that will be irrelevant to the problem

with open('board.txt', 'w') as filehandle:
    for listitem in current_position:
        filehandle.write('"%s", ' % listitem)

z = open(r"board.txt", "r")
print(z.read())    
My array end up looking like this when I read …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
39
查看次数

标签 统计

python ×1