python字符串单个替换_如何用变量替换列表中的单个字符串?
我正在編寫一個腳本,用python為程序maya創建一個命名約定。
我將使用它來命名腳本創建的所有對象。在
例如,以左膝關節為例。腳本會傳遞這樣的信息
(“bind”,“shoulder”,“left”,“joint”)到另一個模塊的變量中
(前綴、名稱、側面、對象類型)。然后,這個輸入將在用戶字典中運行,以檢查現有值并更改為新值,如果找不到任何值,則返回原始值。例如,“joint”會變成“jnt”。在
用戶將輸入如下內容(prefix_x,name_x,SIDE_u,obj_type,01)
我需要它來檢查變量名中是否存在用戶輸入中的任何內容。例如,如果它在用戶輸入中找到任何變量名(例如“prefix”),則替換變量前綴中包含的任何內容,并將其放在其所在位置的索引處。此外,在字符串中找不到的任何內容都將被保留,例如“01”,它將簡單地添加到每個名稱上。在
例如,上面的命令將返回這個“bn_should_jnt01”
另外,如果某些東西是大寫的,比如第一個字母,或者所有的字母,我希望它能自動將傳遞的字母大寫。這就是為什么輸入SIDE_u會把“l”變成“l”。在
我希望它盡可能靈活,但是我當前的問題是如果它找到值,就讓它將變量替換為現有的字符串。我試著想了幾件事,但沒想到太多。這是我的代碼:
另外,我當前將用戶的輸入傳遞到init中的類。它能在整個模塊中工作和使用嗎?我仍然不能百分之百地確定如何使用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()
現在得到這個錯誤,不知道該怎么解釋它:
捆綁,肩,左,關節
回溯(最近一次呼叫):
文件“C:\Users\Gregory\Documents\Gregory's Folder\Artwork\3D Scripts\U MyScripts\neoAutoRig\Scripts\test\neo”_名稱.py“,第19行,英寸
用戶界面測試()
文件“C:\Users\Gregory\Documents\Gregory's Folder\Artwork\3D Scripts\U MyScripts\neoAutoRig\Scripts\test\neo”_名稱.py,第17行,在ui測試中
名稱.轉換(“綁定”、“肩部”、“左側”、“關節”)
文件“C:\Users\Gregory\Documents\Gregory's Folder\Artwork\3D Scripts\U MyScripts\neoAutoRig\Scripts\neo_名稱.py“,第133行,轉換中
self.new_字符串.substitute(前綴=前綴,名稱=名稱,側面=側面,對象類型=對象類型)
文件“C:\Program Files\Autodesk\Maya2014\bin\python27.zip\字符串.py“,第172行,替換
文件“C:\Program Files\Autodesk\Maya2014\bin\python27.zip\字符串.py“,第169行,轉換中
文件“C:\Program Files\Autodesk\Maya2014\bin\python27.zip\字符串.py“,第146行,in\u無效
值錯誤:字符串中的占位符無效:第1行,第38列
總結
以上是生活随笔為你收集整理的python字符串单个替换_如何用变量替换列表中的单个字符串?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python ddt mysql_Pyt
- 下一篇: python编程100行_自己动手写10