终端上的Python-Result重新出现

时间:2013-11-18 11:15:58

标签: python ubuntu terminal

我正在使用Ubuntu 12.04 ..当我运行我的python程序时,终端上的结果会重复出现。详细说来,当我第二次运行我的程序时,第一次执行的结果再次发生..为什么会这样???

我使用命令python myprogram.py来运行我的程序,我的代码如下:

import re
import string
import sys


class parser(object):

    def __init__(self):
        self.fp = open('jsondata.txt', 'r')
        self.fstr = self.fp.read()
        print "Name of the file Opened is :", self.fp.name
        print "Contents of the file :\n", self.fstr
        self.rule1(self.fstr)

    def rule1(self, fstr):
        if self.fstr[:1].startswith('{') and self.fstr[:-1].endswith('}'):
                print "RULE 1\n"
                print "first character is '{' last character is '} :\
PASSED RULE 1\n"
                self.fstr = self.fstr[1:-2]
                print "String to be passed on to next block:\n", self.fstr
                self.splitdata(self.fstr)
        else:
            print "Not a JSON data"

    def splitdata(self, fstr):
        print "Split Data Block\n"
        print "String recieved:\n", self.fstr, "\n"
        self.str1 = self.fstr
        #print self.str1
        self.nested = []
        if self.str1.find('{') != -1 and self.str1.find('}') != -1:
            self.substr1 = \
                self.str1[self.str1.index('{'):self.str1.index('}')+1]
            #print self.substr1
            self.substr = self.substr1[1:-1]
            #print self.substr
            print "String to be passed on to next block:\n", self.substr, "\n"
            self.nested.extend(self.splitdata2(self.substr))
            print "JSON NESTED:", self.nested
            if self.nested is not None:
                if self.substr1 in self.str1:
                    self.nested_op =\
                        self.str1.replace(self.substr1, 'Checked Json')
                    #print self.nested_op
                    self.fstr = self.nested_op
                    self.splitdata(self.fstr)

        else:
            self.substr = self.fstr
            self.splitdata2(self.substr)

        #Storing the words of string in a list
    def splitdata2(self, substr):
        self.splitstr = self.substr
        print "String After splitting:\n", self.splitstr, "\n"
        self.list1 = []
        self.list2 = []
        self.list3 = []
        self.list1 = re.split(',', self.splitstr)
        print "String stored in the list in the following format:\n",\
            self.list1, "\n"
        for i in self.list1:
            self.list2 = list(re.split(':', i))
            self.list3.extend(list(self.list2))
        print "Splitted List contents based on colon:\n", self.list3, "\n"
        self.left_list = []
        self.right_list = []
        self.i = 0
        self.left_list = \
            list([self.list3[i] for i in range(len(self.list3)) if i % 2 == 0])
        self.right_list = \
            list([self.list3[i] for i in range(len(self.list3)) if i % 2 == 1])
        print "DATA SPLIT"
        print "Keys in the json data:\n", self.left_list, "\n"
        print "Values in the json data:\n", self.right_list, "\n"
        self.left_res = []
        self.right_res = []
        self.left_res = self.left_parse()
        print "Validated Json key list:\n", self.left_res, "\n"
        self.right_res = self.right_parse()
        print "Validated Json Values List:\n", self.right_res, "\n"
        return self.left_res, self.right_res

    def left_parse(self):
        """we gona check "This part of the string":\
        "This part will be checked in next function"\
        Conditions imposed on left part of string:\
        1.starts and ends with ""\
        2.Starts with Alphabet\
        3.Contains No special characters except underscore _"""
        self.lvl = []
        for i in range(len(self.left_list)):
            self.leftstr = self.left_list[i]
            if self.leftstr.startswith('"') and self.leftstr.endswith('"')\
                    and self.leftstr[1].isalpha():
                self.leftstr1 = self.leftstr[1:-1]
                if re.match("^[a-zA-Z0-9_]*$", self.leftstr1):
                    self.lvl.append(self.leftstr1)
            else:
                print "Error in json key,Not a Valid Json"
                sys.exit()
        return self.lvl

    def right_parse(self):
        self.rvl = []
        for i in range(len(self.right_list)):
            self.rightstr = self.right_list[i]
            if self.rightstr.startswith('"') and self.rightstr.endswith('"'):
                self.rightstr1 = self.rightstr[1:-1]
                if re.match("^[a-zA-Z0-9_]*$", self.rightstr1):
                    self.rvl.append(self.rightstr1)
            elif re.match("^[a-zA-Z0-9_]*$", self.rightstr):
                    self.rvl.append(self.rightstr)
            else:
                print "Error in Json value,Not a Valid Json"
                sys.exit()
        return self.rvl


obj = parser()

0 个答案:

没有答案