简体   繁体   English

如何从导入的python文件中获取列表

[英]How do I get list from imported python file

This was my python file, and I want to move “names” list to other file, and import it.这是我的 python 文件,我想将“名称”列表移动到其他文件,并导入它。

name = [“one”,
        “two”,
        “three”]
for doing_job in name:
        (doing something)

I have imported sucsessfully, but I can't get that “name” list the main python file.我已成功导入,但我无法获取主 python 文件的“名称”列表。
made like this做成这样

main.py主文件

from settings import name
for doing_job in name

settings/name.py设置/名称.py

name = [“one”,
        “two”,
        “three”]
return name

How can I fix this and get “name” on the main.py file?我该如何解决这个问题并在 main.py 文件中获取“名称”?
Error now is现在的错误是

for doing_job in name:  
NameError: name 'name' is not defined

You can get the name variable by calling name.name .您可以通过调用name.name来获取 name 变量。 Check the example below:检查下面的例子:

Main.py file:主.py文件:

from settings import name

for n in name.name:
    print(n)

settings/name.py file:设置/名称.py 文件:

name = [["one","two","three"]]

Everything is ok unless the curly quotes use straight ones, also it seeems you are missing a ending bracket and why a "useless" return outside function?一切都很好,除非卷曲引号使用直引号,而且似乎您缺少结束括号以及为什么在函数外部返回“无用”?

name = [['one',
        'two',
        'three']]

You can use double "" too您也可以使用双""

main.py主文件

from settings import name
for doing_job in name.name:
    print(doing_job)

or或者

from settings.name import name
for doing_job in name:
    print(doing_job)

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

相关问题 如何获取 Python 导入的库的文件位置 - How do I get the file location of libraries imported by Python 如何从导入文件中的函数导入列表? - How do I import a list from a function in an imported file? 如何在 Python 中列出导入的类? - How do I make a list of imported classes in Python? 如何删除从 Python 中的文件导入的列表周围的双引号? - How can I remove double quotations surrounding a list imported from a file in Python? Pyhton 3.9 如何从导入的 python 文件更新列表 - Pyhton 3.9 How to update a list from an imported python file 如何修补另一个Python文件导入的对象? - How do I patch object imported by another python file? Python:如何获取带有名称列表、两列格式的导入文本文件,并将它们放入按字母顺序排序的 Python 中? - Python: How do I take an imported text file with a list of names, two column format, and put them in python sorted alphabetically? 如何让python从导入的txt文件中检测字典中的空格? - How to get python to detect a space in a dictionary from an imported txt file? 对从文件导入的列表进行排序…Python - Sort a list that was imported from a file… Python 如何从 Python 中的另一个导入函数调用 globals()? - How do I call globals() from another imported function in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM