Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在“中重写字典属性”;设置.py";_Python_Parsing_Dictionary - Fatal编程技术网

Python 在“中重写字典属性”;设置.py";

Python 在“中重写字典属性”;设置.py";,python,parsing,dictionary,Python,Parsing,Dictionary,有没有类似pythonic的方法来重写像settings.py这样的文件中的字典值 有时,使用一些设置创建一个*.py文件很有用。在mysettings.py中,有一个带有键及其值的通用词典。例如,下载停止的位置。在本例中,我想重写此词典,以便能够从此步骤继续 设置。py: settings = {'driver':'firefox', 'last_processed_file':'x1.txt'} settings = {'driver':'firefox',

有没有类似pythonic的方法来重写像
settings.py
这样的文件中的字典值

有时,使用一些设置创建一个
*.py
文件很有用。在my
settings.py
中,有一个带有
键及其
值的通用
词典
。例如,下载停止的位置。在本例中,我想重写此
词典
,以便能够从此步骤继续

设置。py:

settings = {'driver':'firefox',
           'last_processed_file':'x1.txt'}
settings = {'driver':'firefox',
           'last_processed_file':'x2.txt'}
如果我想将“x1.txt”重写为另一个文件/字符串,该怎么办?当然,我可以使用一些不舒服的解析或将字典保存为对象来实现这一点,但两者都有缺点

因此,如果我想更改“last_processed_file”的值,我会执行类似设置的操作。更改_值(“last_processed_file”,“x2.txt”),结果将被重写为file
settings.py

设置。py:

settings = {'driver':'firefox',
           'last_processed_file':'x1.txt'}
settings = {'driver':'firefox',
           'last_processed_file':'x2.txt'}

可能有点傻,但您不能将字典的字符串表示形式转储回您的
设置.py
(如果您只有“简单”的原因值):


您可以将字典pickle转储到一个文件中,然后从一个文件中加载是的,但是在这种情况下,我和任何人都无法更改值(如在django settings.py中)。您始终可以再次加载和更改值转储为什么不将字典声明部分从该文件中取出到其他文件(例如data.json)?那么这个设置映射应该只是读取这个文件,而不是声明一个映射。settings=read_data(“data.json”)data.json将是{'driver':'firefox','last_processed_file':'x2.txt'},然后当您希望更改文件时,您只需将文件读取到内存中的对象,然后更改值并将其写回,而无需更改python文件。**将文件读取到内存中的对象,然后更改值并将其写回,而无需更改python文件。如果您希望在执行过程中进行更新,那么您可以将信号处理程序注册到代码中(比如sighup to reload)