python - 如何将嵌套列表的字符串转换回嵌套列表?

标签 python string nested-lists

抱歉,如果这违反了任何规则,这是我第一次在这里发帖。我们正在进行的项目是用 Python 创建 Connect 4。我们目前正在努力解决如何保存和加载游戏的问题。

到目前为止,我们所做的是将 2D 列表保存在 .txt 文件中,并尝试通过读取它来加载游戏。我们遇到的问题是,当您尝试读取文件时,它读取为字符串而不是列表。例如:

[['R', 'R', 'Y', 'R', 'Y', 'Y', 'Y'], ['Y', 'Y', 'R', 'R', 'R', 'Y', 'Y'], ['R', 'R', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E']]

它被保存为字符串,我们希望将其转换回以用于将保存的点放回到它们所属的位置。

def save():
    global GAME_LIST
    save_file = open("savedGame.txt","w")
    print('Game', GAME_LIST)
    save_file.write(str(GAME_LIST))


#Will basically be the new def main() once you load a file.
def load():
    global PIECES
    savedFile = open("savedGame.txt","r")
    loadedState = savedFile.readline()
    print(loadedState)
    grid()
    PIECES = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0}

def newRed(column):
    coordinates = {'1': -210, '2': -140, '3': -70, '4': 0, '5': 70,        '6': 140, '7': 210}
    red = turtle.Turtle()
    red.hideturtle()
    red.up()
    #Pieces * Cell length for Y value
    red.goto(coordinates[column], -160 + (PIECES[column] * 70))
    PIECES[column] += 1
    red.dot(60, 'red')

def newYellow(column):
    coordinates = {'1': -210, '2': -140, '3': -70, '4': 0, '5': 70, '6': 140, '7': 210}
    #Computer turtle
    yellow = turtle.Turtle()
    yellow.hideturtle()
    yellow.up()
    yellow.goto(coordinates[column], -160 + (PIECES[column] * 70))
    PIECES[column] += 1
    yellow.dot(60, 'yellow')

def toList(stringState):
    sublist = []
    for characters in stringState:
        sublist.append(characters)
    print(sublist)
    return sublist

def reDot(loadedState):
    global ROW
    global COLUMN
    for sortList in range(ROW):
        newList = loadedState[sortList]
        for sortSubList in range(COLUMN):
            sortSubList = int(sortSubList)
            if newList[sortSubList] == "R":
                newRed(sortSubList + 1)
            elif newList[sortSubList] == "Y":
                newYellow(sortSubList + 1)

newList = toList(loadedState)
reDot(newList)

这是我们的代码片段,供引用。 reDot() 应该获取“R”/“Y”的位置并在那里放置一个点。

最佳答案

如果你想将数据保存到文件中并读回它们,我认为JSON库会对你有帮助,非常容易使用,这样:

data = [['R', 'R', 'Y', 'R', 'Y', 'Y', 'Y'], ['Y', 'Y', 'R', 'R', 'R', 'Y', 'Y'], ['R', 'R', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E']]
with open('game_data.json', 'w') as fp:
                    json.dump(data, fp)

然后当你想读回来时:

with open('game_data.json', 'r') as fp:
                data = fp.read()

您可能还想用 try- except block 包装上述代码,以防找不到文件或出现任何其他异常

关于python - 如何将嵌套列表的字符串转换回嵌套列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909014/

相关文章:

python - Python 中的 Defacto 模板应用程序

python - Pandas 处理 numpy timedelta64[ms]

python - 我的 .bat 文件在使用 `python` 后无法读取行

c - 访问冲突读取位置 0x73726573

java - 将正则表达式构造为 Java 字符串

python - 在保持结构的同时重置嵌套列表

python - 如何判断一个数中的连续数字是偶数还是奇数?

Python 简单循环并行化 Jupyter Notebook

c# - 这个字符串有什么问题?

Python-删除二维列表中的列