Javascript Python:检查括号之间是否有字符串

Javascript Python:检查括号之间是否有字符串,javascript,python,refactoring,Javascript,Python,Refactoring,我正在编写一个python脚本,以在文件中排序干净的代码。。主要是Javascript文件,所以到目前为止,我所做的是删除换行符并在每个分号后结束该行,但我想做的是检查字符串是否在括号之间,如果不是,那么我想在分号后结束该行,如果是,我想不理它,继续。。我猜我需要用读台词?这是我所说的一个输出示例 for(var c=clus.length-1; 0<=c; c--){var e=clus[c]; 很抱歉我之前的回答,我没有完全理解你的问题 因此,在我看来,最好的方法是实现一个堆栈。

我正在编写一个python脚本,以在文件中排序干净的代码。。主要是Javascript文件,所以到目前为止,我所做的是删除换行符并在每个分号后结束该行,但我想做的是检查字符串是否在括号之间,如果不是,那么我想在分号后结束该行,如果是,我想不理它,继续。。我猜我需要用读台词?这是我所说的一个输出示例

for(var c=clus.length-1;

0<=c;

c--){var e=clus[c];

很抱歉我之前的回答,我没有完全理解你的问题

因此,在我看来,最好的方法是实现一个堆栈。请参阅有关堆栈的详细说明

因此,使用堆栈时要记住的重要一点是后进先出的概念LIFO代表后进先出。想象一下,在一家餐馆里,你总是把盘子放在最上面,而不是最下面

概念 现在把它放在你特定的上下文中,堆栈将作为一种方式来判断你是否处于妄想之间。可以逐行和逐字符分析文本。每次遇到打开的
“(“
字符时,将
True
布尔值推送到堆栈中。同样,每次遇到关闭的
字符时,将从堆栈中取出(
pop()
)一个值


例子 想象一下:

for(var c=clus.length-1; 0<=c; c--)
将评估为错误;因为你的堆栈不是空的,这意味着你处于两个论题之间

当遇到结束字符“””“”时,只需在堆栈顶部弹出(取出)一个值。 现在,下面的表达式:

if paranthesesDetector.isEmpty()
if paranthesesDetector.isEmpty()
由于堆栈为空,将计算为
True
这意味着你不再处于妄想之间

下面是我制作的堆栈实现的一个简单示例:

import re, getpass, time

curUser=str(getpass.getuser())

dateOfUse=str(time.strftime('%x'))


class Stack:                                #Implementation of stack
                            #https://interactivepython.org/runestone/static/pythonds/BasicDS/ImplementingaStackinPython.html
     def __init__(self):
         self.items = []

     def isEmpty(self):
         return self.items == []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)


def copy(data):
    """
        Function that copies text contained within 'data'
        to a destination text file
    """
    with open('./generated2.js', 'w') as FileToWrite:
        FileToWrite.write('/* Cleaned ' + dateOfUse + ' */\n\n')
        i = 0
        for each in data:       #Write each line in the destination file
            FileToWrite.write(each)



with open('script.js', 'r') as FileToRead:
    paranthesesDetector = Stack()                   #Instantiate Stack object
    data = []
    for eachLine in FileToRead:                     #Loop that reads every line and appends them to a list
        for eachChar in eachLine:
            if eachChar == "(":
                paranthesesDetector.push(True)
            elif eachChar == ")":
                paranthesesDetector.pop()

        if paranthesesDetector.isEmpty():           #Detect if between parantheses
            pass                                    #HERE do whateber you want

copy(data)          #Copy the data contained in the 'data' list to the destination file

你最好使用一个已经存在的格式化程序:当你写清理过的文本时,它会覆盖整个文件,或者取消数据写入?我仍在试图弄清楚这是如何工作的。不,它不会覆盖,因为在
copy()
函数中,当您使用
语句进入文件打开上下文,即
时,首先写入“Cleared…”文本,但不会关闭文件,因为您仍然在
with
中。然后,使用
for
循环复制数据。只有这样,您才能退出“with”并关闭该文件。这不是重要的部分tho,我只是做了一个愚蠢的例子,从一个文件复制数据到另一个文件的方法几乎是无限的。我一直试图测试和理解你的例子,但我有点卡住了,如果我只是运行它,它什么也不输出,我在isEmpty()的if条件下通过了:并放置一个if条件来检查eachChar是否=“;”,然后运行data.append('\n'),但仍然为空
import re, getpass, time

curUser=str(getpass.getuser())

dateOfUse=str(time.strftime('%x'))


class Stack:                                #Implementation of stack
                            #https://interactivepython.org/runestone/static/pythonds/BasicDS/ImplementingaStackinPython.html
     def __init__(self):
         self.items = []

     def isEmpty(self):
         return self.items == []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)


def copy(data):
    """
        Function that copies text contained within 'data'
        to a destination text file
    """
    with open('./generated2.js', 'w') as FileToWrite:
        FileToWrite.write('/* Cleaned ' + dateOfUse + ' */\n\n')
        i = 0
        for each in data:       #Write each line in the destination file
            FileToWrite.write(each)



with open('script.js', 'r') as FileToRead:
    paranthesesDetector = Stack()                   #Instantiate Stack object
    data = []
    for eachLine in FileToRead:                     #Loop that reads every line and appends them to a list
        for eachChar in eachLine:
            if eachChar == "(":
                paranthesesDetector.push(True)
            elif eachChar == ")":
                paranthesesDetector.pop()

        if paranthesesDetector.isEmpty():           #Detect if between parantheses
            pass                                    #HERE do whateber you want

copy(data)          #Copy the data contained in the 'data' list to the destination file