obsidian/笔记文件/2.笔记/python判断特定字符串在字符中的数量.md
2025-03-26 00:02:56 +08:00

1013 B

#python

需求:检测文本的括号是否匹配:

举例说明,获得左括号的数量:

!Pasted image 20230214191402.png

源码:

from inspect import getcomments

import os

from pickle import TRUE

from re import S

import sys

import json

import re

  

svnfile = open("./hotfix.txt",'r',encoding= "utf-8")

start = False

leftcount = 0

rightCount = 0

txt = ""

lalal = svnfile.readlines()

for i in range(len(lalal)):

  line = lalal[i]

  

  if(line.__contains__("regionstart")):

    start = True

  if(line.__contains__("regionend")):

    start = False

  
  

  if(start == True and "(" in line):

    leftcount = leftcount + len(line.split('('))-1

  if(start == True and ")" in line):

    rightCount = rightCount + len(line.split(')'))-1

  

  if(start == False):

    if(leftcount == rightCount):

      leftcount = 0

      rightCount = 0

    else:

      print("leftcount:"+str(leftcount))

      print("rightCount:"+str(rightCount))

      print("txt:"+lalal[i])