简体   繁体   English

通过字符串自动化 python 脚本?

[英]Automate a python script through strings?

let say that thoses python objects below are locked we just cannot change the code, all we can is writing right after it.假设下面的那些 python 对象被锁定,我们无法更改代码,我们只能在它之后编写。 i know it's aweful.我知道这太可怕了。 but let say that we are forced to work with this.但是可以说我们被迫处理这个问题。

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"
#let say that there's 99 of them

How to print the name of each and single one of them (99) witouth repetition?如何在不重复的情况下打印每个和单个(99)的名称?

from my noob perspective.从我的菜鸟的角度来看。 the ideal way to resolve this case witouth repetition is using the same logic that we have with strings.在不重复的情况下解决这种情况的理想方法是使用与字符串相同的逻辑。

Because name => Name+index so it can be really easy to iterate with them.因为 name => Name+index所以很容易用它们进行迭代。

so somewhat a code that work in the same logic of the totally fictive one below:所以有点像下面完全虚构的代码一样的代码:

for i in range (1,100):
    print(Name+f"{i:02d}")
for i in range (1,100):
     string_v_of_obj = "Name" + str(f"{i:02d}")
     print(func_transform_string_to_code(string_v_of_obj))

maybe something like that is possible.也许这样的事情是可能的。

for python_object in script_objects:
    if Name in python_object:
       print(python_object)

This could do the trick:这可以解决问题:

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"

vars = locals().copy()
for i in vars:
    if 'Name' in i:
        print((i, eval(i)))

alternative in one line:一行中的替代方案:

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"

print([(i, eval(i)) for i in locals().copy() if "Name" in i])

You can access the global variables through globals() or if you want the local variables with locals() .您可以通过globals()访问全局变量,或者如果您想要使用locals()访问局部变量。 They are stored in a dict .它们存储在dict中。 So所以

for i in range (1,100):
    print(locals()[f"Name{i:02d}"])

should do what you want.应该做你想做的。

You can use exec:您可以使用执行:

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"
for i in range(1,5):
    num = f"{i:02d}" # python3.6+
    num = "{0:02d}".format(i) # python 3.x
    num = "%02d"%i # python 2
    exec('print(Name'+num+')')

Using this code, as per requirement, you will just print names without printing duplicates:使用此代码,根据要求,您将只打印名称而不打印重复项:

Name01 = "Dorian"
Name02 = "Tom"
Name03 = "Tom"
Name04 = "Jerry"
Name05 = "Jessica"
Name06 = "Jessica"

vars = locals().copy()

names = []

for variable in vars:
    # This will make sure that we are only
    # using variables having 'Name' in their names
    if 'Name' in variable: 
        # Getting the variable's value
        value = eval(variable)
        # If the value is not duplicate
        if value not in names:
            # Append it to our final names list
            names.append(value)

for name in names:
    print (name)

Output Output

Dorian
Tom
Jessica
Jerry

Explanation解释

The locals() function returns a dictionary containing the variables defined in the local namespace. locals() function 返回一个字典,其中包含在本地命名空间中定义的变量。 Calling locals() in the global namespace is same as calling globals() and returns a dictionary representing the global namespace of the module.在全局命名空间中调用 locals() 与调用 globals() 相同,并返回一个表示模块全局命名空间的字典。

use eval()使用eval()

>>> Name01 = "Dorian"
>>> Name02 = "Tom"
>>> Name04 = "Jerry"
>>> Name03 = "Jessica"
>>> for i in range(1, 100):
...   print(eval('Name%02d'%i))
... 
Dorian
Tom
Jessica
Jerry

incase if you are using 3.7+ you can go with f string如果您使用的是 3.7+,则可以使用f string go

f"Name{i:02d}"

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

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