回顾一下以前的笔记,便于以后的查看,以ANSA二次开发作为基础,记录下用到的基础知识。
字符串、列表、字典将以修改part的名字为例进行演示。
import ansa
from ansa import base
from ansa import constants
part = base.GetFirstEntity(constants.NASTRAN,"ANSAPART")
vals = ('Name', 'Module Id')
ret = base.GetEntityCardValues(constants.NASTRAN,part, fields=vals)
oldname = ret['Name']
print (oldname)
# 固定字符串,测试时可以将该行注释
oldname = "HWCOLOR PROP 1001199 24"
cleaned = oldname.strip("HWCOLOR") # " PROP 1001199 24"
vals = {'Name':cleaned,}
# ANSA命名是会自动将首位的空格字符移除
base.SetEntityCardValues(constants.NASTRAN, part, vals)
这是基础的修改代码,后续代码皆可通过替换上述代码的字符串处理行进行测试
cleaned = oldname.strip("HWCOLOR")
print(cleaned) # " PROP 1001199 24"
# 去除左侧空白:
print(" HWCOLOR PROP ".lstrip()) # 输出: 'HWCOLOR PROP 1001199 24'
#去除右侧空白:
print(" HWCOLOR PROP ".rstrip()) # 输出: 'HWCOLOR PROP 1001199 24'
parts = oldname.split()
print(parts) # ['HWCOLOR', 'PROP', '1001199', '24']
replaced = oldname.replace("1001199", "0000000")
print(replaced) # "HWCOLOR PROP 0000000 24"
position = oldname.find("PROP")
print(position) # 8
upper_name = oldname.upper()
print(upper_name) # "HWCOLOR PROP 1001199 24"
lower_name = oldname.lower()
print(lower_name) # "hwcolor prop 1001199 24"
starts_with = oldname.startswith("HW")
print(starts_with) # True
ends_with = oldname.endswith("24")
print(ends_with) # True
is_digit = oldname.isdigit()
print(is_digit) # False
length = len(oldname)
print(length) # 27
10. 索引
访问字符串中的特定位置
print(oldname[0]) # 输出: 'H'
11. 切片
获取字符串的一部分
print(oldname[0:7]) # 输出: 'HWCOLOR'
12. capitalize()
首字母大写:
print(oldname.capitalize()) # 输出: 'Hwcolor prop 1001199 24'
13. title()
每个单词首字母大写:
print(oldname.title()) # 输出: 'Hwcolor Prop 1001199 24'
14. 连接字符串:
words = ['HWCOLOR', 'PROP', '1001199', '24']
print(" ".join(words)) # 输出: 'HWCOLOR PROP 1001199 24'