首页/文章/ 详情

ANSA二次开发_Python基础-字符串

2月前浏览2930

回顾一下以前的笔记,便于以后的查看,以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)# 固定字符串,测试时可以将该行注释oldname = "HWCOLOR PROP            1001199      24"cleaned = oldname.strip("HWCOLOR") # " PROP 1001199 24"vals = {'Name':cleaned,}# ANSA命名是会自动将首位的空格字符移除base.SetEntityCardValues(constants.NASTRAN, part, vals)

    这是基础的修改代码,后续代码皆可通过替换上述代码的字符串处理行进行测试

    1. strip()
    去除字符串两端的空格(或指定字符)。
      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'
      2. split()
      根据指定的分隔符将字符串分割成一个列表。
        parts = oldname.split()print(parts)  # ['HWCOLOR', 'PROP', '1001199', '24']
        3. replace()
        替换字符串中的某些部分。
          replaced = oldname.replace("1001199", "0000000")print(replaced)  # "HWCOLOR PROP 0000000 24"
          4. find()
          查找子字符串第一次出现的位置。
            position = oldname.find("PROP")print(position)  # 8
            5. upper()
            转换字符串的大写。
              upper_name = oldname.upper()print(upper_name)  # "HWCOLOR PROP 1001199 24"
              6. lower()
              转换字符串的小写。
                lower_name = oldname.lower()print(lower_name)  # "hwcolor prop 1001199 24"
                7. startswith(), endswith()
                检查字符串是否以指定的子字符串开始或结束。
                  starts_with = oldname.startswith("HW")print(starts_with)  # Trueends_with = oldname.endswith("24")print(ends_with)  # True
                  8. isdigit()
                  检查字符串是否只包含数字。
                    is_digit = oldname.isdigit()print(is_digit)  # False
                    9. len()
                    获取字符串长度。
                      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'
                                来源:CAEer
                                Nastran二次开发ANSApython
                                著作权归作者所有,欢迎分享,未经许可,不得转载
                                首次发布时间:2024-03-04
                                最近编辑:2月前
                                团长
                                硕士 | 白车身结构设... 专注ANSA使用技巧-*****『CAEer』...
                                获赞 101粉丝 836文章 123课程 6
                                点赞
                                收藏
                                未登录
                                还没有评论

                                课程
                                培训
                                服务
                                行家

                                VIP会员 学习 福利任务 兑换礼品
                                下载APP
                                联系我们
                                帮助与反馈