[英]How do I regex search for python values? “”“string”“”, “string”, 'string', (tuple), [list], {dict}
I'm trying to extract variables/values from a Django settings.py
file (preserving comments, whitespace, etc.) and move or append them to another file programmatically using Python. 我正在尝试从Django settings.py
文件中提取变量/值(保留注释,空格等),然后使用Python以编程方式将其移动或附加到另一个文件中。 Overall, I'm trying to split up the settings module into several files automatically (not manually using a text editor). 总体而言,我正在尝试将设置模块自动拆分为多个文件(而不是使用文本编辑器手动进行)。
How do I find the value assigned to any variable in such a file? 如何在这样的文件中找到分配给任何变量的值? - If possible in a concise way, eg using regular expressions. -如果可能的话,以简洁的方式,例如使用正则表达式。
Example variables/values: 示例变量/值:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = [
'*'
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_PATH, 'database.sqlite'),
}
}
SECRET_KEY = '#*ji*0hbpj6#4-0=$%bl(z+z)c=pd(rn^9u*1_96f^ba+4w58v'
LONG_STRING_VALUE = \
"""
bla foo bar baz ...
"""
A_TUPLE = (
'...',
)
Notes: 笔记:
Partially working code is on GitHub: DjangoSettingsManager (identifies any variable until after equal sign, as of 15-feb-2014) 部分有效的代码在GitHub上: DjangoSettingsManager (标识所有变量,直到等号后,截至2014年2月15日)
r'([ ]*#.*\\n)*[ ]*(\\A|\\b)' + var + r'\\s*=\\s*\\\\?\\s*'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.