简体   繁体   English

我可以在 Python 中创建一个数据文件并将其导入其他 python 文件吗

[英]Can I create a data file in Python and import it to other python files

I have created a python file that consists some data.我创建了一个包含一些数据的 python 文件。 I m able to import this data file to a different python file, but my concern is that I can import only one variable at a time and not all the variables at once.我可以将此数据文件导入到另一个 python 文件,但我担心我一次只能导入一个变量,而不是一次导入所有变量。

from sourcefilename import RequiredVariable

I want to import and use all the variables in one call.我想在一次调用中导入和使用所有变量。 Is that possible.那可能吗。

from sourcefilename import *

This is really not recommended, though.不过,这真的不推荐。 It lets you imports (essentially, execute) any arbitrary code from the module sourcefilename , and then simply copy all global names from that module into your module (overwriting anything previously defined in your module).它允许您从模块sourcefilename导入(本质上是执行)任何任意代码,然后只需将该模块中的所有全局名称复制到您的模块中(覆盖之前在模块中定义的任何内容)。 You should use it only if you can be very certain of the exact data structure that goes into sourcefilename .只有当您可以非常确定进入sourcefilename的确切数据结构时,您才应该使用它。 However, if that is the case, you may as well use a more conventional method for storing data, like JSON or pickle .但是,如果是这种情况,您也可以使用更传统的方法来存储数据,例如JSONpickle

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

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