简体   繁体   English

将字典导出到文本文件

[英]Exporting a dictionary into a text-file

The game is about tamaguchis, and I want the tamaguchi to remember its last size and it's 3 last actions the next time it plays. 游戏是关于tamaguchis的游戏,我希望tamaguchi记住它的最后一个尺寸,并且下次播放时它是最后3个动作。 I also want the date to matter, like if you don't play with it for a week it shrinks in size. 我也希望日期很重要,例如,如果您一周不玩它,它的大小就会缩小。 So first step I thought was to save down all the relevant data to a text file, and each time the game starts the code searchs through the text file and extracts the relevant data again! 所以我想的第一步是将所有相关数据保存到文本文件中,并且每次游戏启动时,代码都会搜索文本文件并再次提取相关数据! But I can't even get step 1 working :( I mean, I don't get why this doesn't work?? 但是我什至无法使步骤1起作用:(我的意思是,我不明白为什么这不起作用?

file = open("Tamaguchis.txt","w")
date = time.strftime("%c")
dictionary = {"size":tamaguchin.size,"date":date,"order":lista}
file.write(dictionary)

it says that it can't export dictonaries, only strings to a text file. 它说它不能导出字典,只能导出字符串到文本文件。 But that's not correct is it, I thought you were supposed to be able to put dictionaries in text files? 但这是不对的,我认为您应该能够在文本文件中添加字典? :o :○

If anyone also has an idea on how calculate the difference between the current date, and the date saved in the text file, that'd be much appriciated :) 如果有人对如何计算当前日期和文本文件中保存的日期之间的差额有想法,那将是非常有用的:)

Sorry if noob question, and thanks a lot! 很抱歉,如果菜鸟有问题,非常感谢!

If your dictionary consists only of simple python objects, you can use json module to serialize it and write into a file. 如果字典仅包含简单的python对象,则可以使用json模块对其进行序列化并写入文件。

import json
with open("Tamaguchis.txt","w") as file:
    date = time.strftime("%c")
    dictionary = {"size":tamaguchin.size,"date":date,"order":lista}
    file.write(json.dumps(dictionary))

The same can be then read with loads . 然后可以读取相同的loads

import json
with open("Tamaguchis.txt","r") as file:
    dictionary = json.loads(file.read())

If your dictionary may contain more complex objects, you can either define json serializer for them, or use pickle module. 如果字典中可能包含更复杂的对象,则可以为它们定义json序列化程序,也可以使用pickle模块。 Note, that the latter might make it possible to invoke arbitrary code if not used properly. 注意,如果使用不当,后者可能使调用任意代码成为可能。

You need to convert the dict to a string: 您需要将字典转换为字符串:

file.write(str(dictionary))

... though you might want to use pickle , json or yaml for the task - reading back is easier/safer then. ...尽管您可能要使用picklejsonyaml来完成任务- yaml之前更容易/更安全。

Oh,, for date and time caculations you might want to check the timedelta module. 哦,对于日期和时间计算,您可能需要检查timedelta模块。

import pickle

a = {'a':1, 'b':2}
with open('temp.txt', 'w') as writer:
    data = pickle.dumps(a)
    writer.write(data)

with open('temp.txt', 'r') as reader:
    data2= pickle.loads(reader.read())
    print data2
    print type(data2)

Output: 输出:

{'a': 1, 'b': 2}
<type 'dict'>

If you care efficiency, ujson or cPinkle is better. 如果您关心效率,则使用ujsoncPinkle更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM