回顾一下以前的笔记,便于以后的查看,以ANSA二次开发作为基础,记录下用到的基础知识。
字符串、列表、字典将以修改part的名字为例进行演示。
import ansafrom ansa import basefrom ansa import constantspart = base.GetFirstEntity(constants.NASTRAN,"ANSAPART")vals = ('Name', 'Module Id')ret = base.GetEntityCardValues(constants.NASTRAN,part, fields=vals)oldname = ret['Name']print (oldname)# 固定字符串,测试时可以将该行注释s = "HWCOLOR PROP 1001199 24"list_from_s = s.split()print(list_from_s)list_from_s.append("NEW_ITEM")print(list_from_s)list_from_s = " ".join(list_from_s)print(list_from_s)vals = {'Name':list_from_s,}# ANSA命名是会自动将首位的空格字符移除base.SetEntityCardValues(constants.NASTRAN, part, vals)
这是基础的修改代码,后续皆可使用上述代码进行测试
s = "HWCOLOR PROP 1001199 24"list_from_s = s.split()print(list_from_s)#使用join方法将列表转化为字符串string = ' '.join(list_from_s )print(string)
list_from_s.append("NEW_ITEM")print(list_from_s)
list_from_s.extend(["ITEM1", "ITEM2"]) #['HWCOLOR', 'PROP', '1001199', '24', 'ITEM1', 'ITEM2']print(list_from_s)
list_from_s.insert(1, "INSERTED_ITEM") #['HWCOLOR', 'INSERTED_ITEM', 'PROP', '1001199', '24']print(list_from_s)
list_from_s.remove("1001199") #['HWCOLOR', 'PROP', '24']print(list_from_s)
popped_item = list_from_s.pop()print(list_from_s) #['HWCOLOR', 'PROP', '1001199']print("被pop出的元素:", popped_item)
list_from_s.clear() #[]print(list_from_s)
index_of_1001199 = list_from_s.index("1001199") #2print("1001199的索引位置:", index_of_1001199) #1001199的索引位置: 2
count_of_24 = list_from_s.count("24") #1print("24出现的次数:", count_of_24)
如果列表元素类型不同,直接调用sort()会报错list_from_s.sort() #['1001199', '24', 'HWCOLOR', 'PROP']print(list_from_s)
list_from_s.reverse() #['24', '1001199', 'PROP', 'HWCOLOR']print(list_from_s)
list_copy = list_from_s.copy()print("列表的浅复制:", list_copy)