在 python 中创建和使用首选项文件
creating and using a preferences file in python
全新堆叠 python;希望比我聪明的人可以提供帮助。我上下搜索了一下,似乎找不到真正的答案,如果有确切的答案而我错过了,我深表歉意:((我发现的几个要么是旧的,要么似乎不是工作)。
我找到的最接近的是
Best way to retrieve variable values from a text file?
las,imp 似乎被贬值了,并尝试弄清楚 importlib 但在我目前的大脑中几乎没有想到如何适应它,因为错误在我身上左右抛出。
这非常接近我想要的,如果有人可以帮助更新新方法,它可能会起作用,可惜仍然不知道如何覆盖旧变量。
= - - 场景 - - =
我想创建一个首选项文件(我们称之为 settings.txt 或 settings.py:不需要与其他语言交叉兼容,但出于某种原因我更喜欢 txt - 任何 preference/standards 编码员可以传授将不胜感激?)。
\\ settings.txt\
water_type = "Fresh"\
measurement = "Metric"\
colour = "Blue"\
location = "Bottom"\
...
我正在创建一个脚本 main_menu.py,它将读取 settings.txt 中的变量,并在 'saved'
发生变化时写入此文件
即。
"select 水类型:"
- 新鲜
- 盐
如果 water_type 与 settings.txt 相同,什么都不做,
如果 water_type 不同,覆盖 settings.txt 文件中的变量
在线下的其他脚本也将读取和写入此设置文件。
我看过:
from settings import *
如果我走 settings.py 路径,这似乎可以读取文件,但仍然让我思考如何覆盖它。
也对你们能想到的任何 better/standard/ideas 开放。
感谢任何帮助!
如果您选择这样做,这里有一个 python library。
如果没有this也是一个很好的资源
创建首选项文件示例
正在将首选项写入来自 python 文件的文件
import json
# Data to be written
dictionary ={
"name" : "sathiyajith",
"rollno" : 56,
"cgpa" : 8.6,
"phonenumber" : "9976770500"
}
# Serializing json
json_object = json.dumps(dictionary, indent = 4)
# Writing to sample.json
with open("sample.json", "w") as outfile:
outfile.write(json_object)
正在从 Python
中的 .json 文件读取首选项
import json
# open and read file content
with open('sample.json') as json_file:
data = json.load(json_file)
# print json file
print(data)
对于 .py 配置文件,通常是静态选项或设置。
例如
# config.py
STRINGTOWRITE01 = "Hello, "
STRINGTOWRITE02 = "World!"
LINEENDING = "\n"
很难以这种格式保存对设置所做的更改。
我推荐 JSON 文件。
例如。 settings.json
{
"MainSettings": {
"StringToWrite": "Hello, World!"
}
}
要将此文件中的设置读入 Python Dictionary,您可以使用这段代码。
import json # Import pythons JSON library
JSON_FILE = open('settings.json','r').read() # Open the file with read permissions, then read it.
JSON_DATA = json.loads(JSON_FILE) # load the raw text from the file into a json object or dictionary
print(JSON_DATA["MainSettings"]["StringToWrite"]) # Access the 'StringToWrite' variable, just as you would with a dictionary.
要写入 settings.json
文件,您可以使用这段代码
import json # import pythons json lib
JSON_FILE = open('settings.json','r').read() # Open the file with read permissions, then read it.
JSON_DATA = json.loads(JSON_FILE) # load the data into a json object or dictionary
print(JSON_DATA["MainSettings"]["StringToWrite"]) # Print out the StringToWrite "variable"
JSON_DATA["MainSettings"]["StringToWrite"] = "Goodnight!" # Change the StringToWrite
JSON_DUMP = json.dumps(JSON_DATA) # Turn the json object or dictionary back into a regular string
JSON_FILE = open('settings.json','w') # Reopen the file, this time with read and write permissions
JSON_FILE.write(JSON_DUMP) # Update our settings file, by overwriting our previous settings
现在,我写这篇文章是为了尽可能容易地理解正在发生的事情。 Python Functions.
有更好的方法
以下是一些可能对您有所帮助的建议:
- 使用 json 文件:
settings.json
{
"water_type": "Fresh",
"measurement": "Metric",
"colour": "Blue",
"location": "Bottom",
...
}
然后在 python:
import json
# Load data from the json file
with open("settings.json", "r") as f:
x = json.load(f) # x is a python dictionary in this case
# Change water_type in x
x["water_type"] = "Salt"
# Save changes
with open("settings.json", "w") as f:
json.dump(x, f, indent=4)
- 使用 yaml 文件:(编辑:您需要安装 pyyaml)
settings.yaml
water_type: Fresh
measurement: Metric
colour: Blue
location: Bottom
...
然后在 python:
import yaml
# Load data from the yaml file
with open("settings.yaml", "r") as f:
x = yaml.load(f, Loader=yaml.FullLoader) # x is a python dictionary in this case
# Change water_type in x
x["water_type"] = "Salt"
# Save changes
with open("settings.yaml", "w") as f:
yaml.dump(x, f)
- 使用 INI 文件:
settings.ini
[Preferences]
water_type=Fresh
measurement=Metric
colour=Blue
location=Bottom
...
然后在 python:
import configparser
# Load data from the ini file
config = configparser.ConfigParser()
config.read('settings.ini')
# Change water_type in config
config["Preferences"]["water_type"] = "Salt"
# Save changes
with open("settings.ini", "w") as f:
config.write(f)
你们真快!我周末不在电脑旁,但不得不登录以表示感谢。
下周我回来时会进一步研究这些内容,并有时间给予它所需的关注。快速浏览一下可能会有点有趣,可以实施并了解更多信息。
- 不得不回答,因为添加评论只是针对你们的一个解决方案,我想感谢所有人!
干杯
全新堆叠 python;希望比我聪明的人可以提供帮助。我上下搜索了一下,似乎找不到真正的答案,如果有确切的答案而我错过了,我深表歉意:((我发现的几个要么是旧的,要么似乎不是工作)。
我找到的最接近的是 Best way to retrieve variable values from a text file? las,imp 似乎被贬值了,并尝试弄清楚 importlib 但在我目前的大脑中几乎没有想到如何适应它,因为错误在我身上左右抛出。 这非常接近我想要的,如果有人可以帮助更新新方法,它可能会起作用,可惜仍然不知道如何覆盖旧变量。
= - - 场景 - - =
我想创建一个首选项文件(我们称之为 settings.txt 或 settings.py:不需要与其他语言交叉兼容,但出于某种原因我更喜欢 txt - 任何 preference/standards 编码员可以传授将不胜感激?)。
\\ settings.txt\
water_type = "Fresh"\
measurement = "Metric"\
colour = "Blue"\
location = "Bottom"\
...
我正在创建一个脚本 main_menu.py,它将读取 settings.txt 中的变量,并在 'saved'
发生变化时写入此文件即。 "select 水类型:"
- 新鲜
- 盐
如果 water_type 与 settings.txt 相同,什么都不做, 如果 water_type 不同,覆盖 settings.txt 文件中的变量
在线下的其他脚本也将读取和写入此设置文件。
我看过:
from settings import *
如果我走 settings.py 路径,这似乎可以读取文件,但仍然让我思考如何覆盖它。
也对你们能想到的任何 better/standard/ideas 开放。
感谢任何帮助!
如果您选择这样做,这里有一个 python library。 如果没有this也是一个很好的资源
创建首选项文件示例
正在将首选项写入来自 python 文件的文件
import json
# Data to be written
dictionary ={
"name" : "sathiyajith",
"rollno" : 56,
"cgpa" : 8.6,
"phonenumber" : "9976770500"
}
# Serializing json
json_object = json.dumps(dictionary, indent = 4)
# Writing to sample.json
with open("sample.json", "w") as outfile:
outfile.write(json_object)
正在从 Python
中的 .json 文件读取首选项import json
# open and read file content
with open('sample.json') as json_file:
data = json.load(json_file)
# print json file
print(data)
对于 .py 配置文件,通常是静态选项或设置。
例如
# config.py
STRINGTOWRITE01 = "Hello, "
STRINGTOWRITE02 = "World!"
LINEENDING = "\n"
很难以这种格式保存对设置所做的更改。
我推荐 JSON 文件。
例如。 settings.json
{
"MainSettings": {
"StringToWrite": "Hello, World!"
}
}
要将此文件中的设置读入 Python Dictionary,您可以使用这段代码。
import json # Import pythons JSON library
JSON_FILE = open('settings.json','r').read() # Open the file with read permissions, then read it.
JSON_DATA = json.loads(JSON_FILE) # load the raw text from the file into a json object or dictionary
print(JSON_DATA["MainSettings"]["StringToWrite"]) # Access the 'StringToWrite' variable, just as you would with a dictionary.
要写入 settings.json
文件,您可以使用这段代码
import json # import pythons json lib
JSON_FILE = open('settings.json','r').read() # Open the file with read permissions, then read it.
JSON_DATA = json.loads(JSON_FILE) # load the data into a json object or dictionary
print(JSON_DATA["MainSettings"]["StringToWrite"]) # Print out the StringToWrite "variable"
JSON_DATA["MainSettings"]["StringToWrite"] = "Goodnight!" # Change the StringToWrite
JSON_DUMP = json.dumps(JSON_DATA) # Turn the json object or dictionary back into a regular string
JSON_FILE = open('settings.json','w') # Reopen the file, this time with read and write permissions
JSON_FILE.write(JSON_DUMP) # Update our settings file, by overwriting our previous settings
现在,我写这篇文章是为了尽可能容易地理解正在发生的事情。 Python Functions.
有更好的方法以下是一些可能对您有所帮助的建议:
- 使用 json 文件:
settings.json
{
"water_type": "Fresh",
"measurement": "Metric",
"colour": "Blue",
"location": "Bottom",
...
}
然后在 python:
import json
# Load data from the json file
with open("settings.json", "r") as f:
x = json.load(f) # x is a python dictionary in this case
# Change water_type in x
x["water_type"] = "Salt"
# Save changes
with open("settings.json", "w") as f:
json.dump(x, f, indent=4)
- 使用 yaml 文件:(编辑:您需要安装 pyyaml)
settings.yaml
water_type: Fresh
measurement: Metric
colour: Blue
location: Bottom
...
然后在 python:
import yaml
# Load data from the yaml file
with open("settings.yaml", "r") as f:
x = yaml.load(f, Loader=yaml.FullLoader) # x is a python dictionary in this case
# Change water_type in x
x["water_type"] = "Salt"
# Save changes
with open("settings.yaml", "w") as f:
yaml.dump(x, f)
- 使用 INI 文件:
settings.ini
[Preferences]
water_type=Fresh
measurement=Metric
colour=Blue
location=Bottom
...
然后在 python:
import configparser
# Load data from the ini file
config = configparser.ConfigParser()
config.read('settings.ini')
# Change water_type in config
config["Preferences"]["water_type"] = "Salt"
# Save changes
with open("settings.ini", "w") as f:
config.write(f)
你们真快!我周末不在电脑旁,但不得不登录以表示感谢。 下周我回来时会进一步研究这些内容,并有时间给予它所需的关注。快速浏览一下可能会有点有趣,可以实施并了解更多信息。
- 不得不回答,因为添加评论只是针对你们的一个解决方案,我想感谢所有人!
干杯