如何用变量替换列表中的单个字符串?

时间:2014-09-25 20:22:25

标签: python list replace maya

我正在创建一个脚本,以便在程序maya的python中创建命名约定。 我将使用它来命名我的脚本创建的所有对象。

例如,我们采取左膝关节。该脚本将传递这样的东西 (“bind”,“shoulder”,“left”,“joint”)将另一个模块转换为变量 (前缀,名称,边,obj_type)。然后,此输入将通过用户词典运行以检查现有值并更改为新值,如果未找到任何内容,则返回原始值。例如,“joint”将变成“jnt”。

用户将输入类似(prefix_,name_,SIDE_,obj_type,01)的内容

我需要它以便检查变量名中是否存在用户输入中的任何内容。例如,如果它在用户输入中找到任何变量名称(例如“prefix”),则将变量前缀中包含的内容替换为它所在的索引。此外,字符串中未找到的任何内容都将保留,例如“01”,它将被简单地添加到每个名称上。

例如,以上内容将返回此“bn_shoulder_L_jnt01”

此外,如果某些内容大写,如第一个字母或所有字母,我希望它自动将所传递的字母大写。这就是为什么输入SIDE_会将“l”变成“L”。

我希望它尽可能灵活,但我目前的麻烦是如果它找到了值,就将变量替换为现有字符串。我已经尝试过想几件事,但还没有想出太多东西。这是我的代码:

此外,我目前已将用户输入传递给 init 中的类。它是否有效并可在整个模块中使用?我仍然不能100%确定如何使用 init ,但我希望当用户输入命名约定时,只要需要它就可以在内存中使用。

编辑代码:

from string import Template

class Name:

    def __init__(self, user_conv):
        self.user_conv = user_conv


    def user_dict(self, word):
        """Matches given word with a dictionary, and returns converted abbreviated word.

        Keyword Arguments:
        string -- given string to be converted
                  example: joint > jnt
        """

        # prefixes
        user_library = {
        'bind' : 'bn',
        'driver' : 'drv',

        # side
        'back' : 'b',
        'down' : 'd',
        'front' : 'f',
        'left' : 'l',
        'right' : 'r',
        'up' : 'u',

        # obj_type
        'cluster' : 'clstr',
        'control' : 'ctrl',
        'curve' : 'crv',
        'effector' : 'efctr',
        'group' : 'grp',
        'ikHandle' : 'ikH',
        'joint' : 'jnt',
        'locator' : 'loc',
        'nurbs' : 'geo',
        'orientConstraint' : 'orCnstr',
        'parentConstraint' : 'prntCnstr',
        'pointConstraint' : 'ptCnstr',
        'polyMesh' : 'geo',

        # utilities
        'addDoubleLinear' : 'adl',
        'blendColors' : 'blndClr',
        'BlendTwoAttr' : 'b2a',
        'chooser' : 'chsr',
        'clamp' : 'clmp',
        'condition' : 'cn',
        'curveInfo' : 'crvI',
        'diffuse' : 'diffuse',
        'displacement' : 'displ',
        'multiplyDivide' : 'mdv',
        'normal' : 'normal',
        'place2d' : 'p2d',
        'plusMinusAverage' : 'pma',
        'reverse' : 'rv',
        'setRange' : 'sr',
        'shader' : 'shdr',
        'shadingGroup' : 'SG',
        'specular' : 'spec',
        'transparency' : 'trans',

        # sequential bones
        'arm' : 'arm',
        'fingser' : 'finger',
        'index' : 'index',
        'leg' : 'leg',
        'limb' : 'limb',
        'middle' : 'middle',
        'pinky' : 'pinky',
        'ring' : 'ring',
        'spine' : 'spine',
        'toe' : 'toe',
        'thumb' : 'thumb',

        #
        'ankle' : 'ankle',
        'ball' : 'ball',
        'breast' : 'breast',
        'chest' : 'chest',
        'clavicle' : ' clavicle',
        'elbow' : 'elbow',
        'end' : 'e',
        'head' : 'head',
        'hair' : 'hair',
        'knee' : 'knee',
        'neck' : 'neck',
        'pelvis' : 'pelvis',
        'root' : 'root',
        'shoulder' : 'shoulder',
        'tail' : 'tail',
        'thigh' : 'thigh',
        'wrist' : 'wrist'
        }

        if word in user_library:
            abbrevWord = user_library[word]
        else:
            abbrevWord = word

        return [abbrevWord]

    def convert(self, prefix, name, side, obj_type):
        """Converts given information about object into user specified naming convention.

        Keyword Arguments:
        prefix -- what is prefixed before the name
        name -- name of the object or node
        side -- what side the object is on, example 'left' or 'right'
        obj_type -- the type of the object, example 'joint' or 'multiplyDivide'
        """
        self.prefix = self.user_dict(prefix)
        self.name = self.user_dict(name)
        self.side = self.user_dict(side)
        self.obj_type = self.user_dict(obj_type)

        print '%s, %s, %s, %s' %(prefix, name, side, obj_type)

        self.new_string = Template (self.user_conv.lower())
        self.subs = {'prefix': prefix, 'name': name, 'side': side, 'obj_type': obj_type}
        self.new_string.substitute(**self.subs)

        print new_string

        return new_string

test code:

    # test file to test naming convention to see if its functioning properly

    import neo_name
    reload(neo_name)

def ui_test():
    """types user can input
    #prefix
    #name
    #side
    #type
    constants (such as 01, present in ALL objects/nodes/etc.)
    """
    user_conv = '${prefix}_${name}_${side}_${obj_type}${01}'

    name = neo_name.Name(user_conv)
    name.convert('bind', 'shoulder', 'left', 'joint')

ui_test()

现在出现此错误,不知道该怎么做:     绑,肩,左,关节     Traceback(最近一次调用最后一次):       文件“C:\ Users \ Gregory \ Documents \ Gregory的Folder \ Artwork \ 3D Scripts_MyScripts \ neoAutoRig \ scripts \ test_neo_name.py”,第19行,in         ui_test()       在ui_test中输入文件“C:\ Users \ Gregory \ Documents \ Gregory的Folder \ Artwork \ 3D Scripts_MyScripts \ neoAutoRig \ scripts \ test_neo_name.py”,第17行         name.convert('bind','shoulder','left','joint')

文件“C:\ Users \ Gregory \ Documents \ Gregory的Folder \ Artwork \ 3D Scripts_MyScripts \ neoAutoRig \ scripts \ neo_name.py”,第133行,转换     self.new_string.substitute(prefix = prefix,name = name,side = side,obj_type = obj_type)   文件“C:\ Program Files \ Autodesk \ Maya2014 \ bin \ python27.zip \ string.py”,第172行,代替   文件“C:\ Program Files \ Autodesk \ Maya2014 \ bin \ python27.zip \ string.py”,第169行,转换   文件“C:\ Program Files \ Autodesk \ Maya2014 \ bin \ python27.zip \ string.py”,第146行,_invalid ValueError:字符串中的占位符无效:第1行,第38列

2 个答案:

答案 0 :(得分:1)

这也是python string.Template类的一个很好的应用程序:

来自字符串导入模板

t = Template ("${prefix}_${side}_${limb}")
t.substitute(prefix ='character', side='R', limb='hand')
>>> 'character_R_hand'

您可以通过将分隔符视为有条件(默认为&#39;&#39;),强制执行大小写规则等来获得更高级别的功能.Steak.substitute可以使用标记(如上例所示)或字典:< / p>

t = Template ("${prefix}_${side}_${limb}")
subs = {'prefix':'dict', 'side':'L', 'limb':'bicep'}
t.substitute(**subs)
>>> 'dict_L_hand'

答案 1 :(得分:0)

您必须制作一个将字符串映射到实际命令的字典

d = {'bind':'bn', 'left':'L', 'joint':'jnt'}
l = ['bind', 'shoulder', 'left', 'joint', '01']

然后使用带有第二个参数的get,如果找不到该键,则使用它,您可以只传递原始参数。然后将所有内容与'_'一起加入。

'_'.join(d.get(i,i) for i in l)

输出

'bn_shoulder_L_jnt_01'

在你的情况下,这将是

'_'.join(userLibrary.get(word,word) for word in prefixes)
相关问题