I am new to python, in the below code i am reading 2 files and writing in 2 files. I want to open these files in seperate functions as to make some indented look. Can anyone suggest me how to do it?

def compare():
    memberReportFile=open("D:\MemberReport.txt", 'w')  
    readableRulesFile=open("D:\ReadableRules.txt", 'w')
    membersFile=open("D:\Members.txt")
    rulesFile=open("D:\Rules.txt", 'r')
    rulesArray=rulesFile.readlines()
    membersArray=membersFile.readlines() 
    for i in range (len(rulesArray)):
        rule= rulesArray[i].split(",")
        rule= rulesArray[i].split(" ")
        if(rule[2].strip()=='GT'):
            if(rule[6].strip()=='GT'):
                if(rule[12].strip()=='GT'):
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in>='
                else:
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in<'
            else:
                if(rule[12].strip()=='GT'):
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in>='
                else:
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in<'
            readableRulesFile.write("Rule " + str(i+1) +": If " + ruleIndex1 + rule[3] + ruleIndex2 + rule[7] + " & Gender = " + rule[10] + ruleIndex3 + rule[13] + rule[14] )
            readableRulesFile.write("\n")
        elif(rule[2].strip()=='LT'):
            if(rule[6].strip()=='GT'):
                if(rule[12].strip()=='GT'):
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in>='
                else:
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in<'
            else:
                if(rule[12].strip()=='GT'):
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in>='
                else:
                    ruleIndex1='Age>='
                    ruleIndex2=' & Win-Loss>='
                    ruleIndex3=' & Log-in<'
            readableRulesFile.write("Rule " + str(i+1) +": If " + ruleIndex1 + rule[3] + ruleIndex2 + rule[7]+ " & Gender = " + rule[10] + ruleIndex3 + rule[13] + rule[14] )
            readableRulesFile.write("\n")
        member= membersArray[i].split(",")
        print rule
        """Rule 1: If Age >= 25 & Win-Loss >= 8 & Gender = Male""" 
        if (member[1]>=25) and (member[2]>=8) and (member[3].strip()=='Male'):
                result = member[0] + " will " + rule[14]
                memberReportFile.write(result)
                memberReportFile.write('\n')
                """Rule 2: If Age >= 25 & Win-Loss < 8 & Gender = Male --> Discontinue."""
        elif(member[1]>=25) and (member[2]<8) and (member[3].strip()=='Male'):
                result = member[0] + " will " + rule[14]
                memberReportFile.write(result)
                memberReportFile.write('\n')
                """Rule 3: If Age >= 25 & Gender = Female --> Discontinue."""
        elif(member[1]>=25) and (member[3].strip() == 'Female'):
                result = member[0] + " will " + rule[14]
                memberReportFile.write(result)
                memberReportFile.write('\n')
                """Rule 4: If Age < 25 & Log-in >= 150 --> Continue."""
        elif(member[1]<25) and (member[4]>=150):
                result = member[0] + " will " + rule[14]
                memberReportFile.write(result)
                memberReportFile.write('\n')
                """Rule 5: If Age < 25 & Win-Loss >= 5 & Log-in <150 --> Continue."""
        elif(member[1]<25) and (member[2]>=5) and (member[4]<150):
                result = member[0] + " will " + rule[14]
                memberReportFile.write(result)
                memberReportFile.write('\n')
                """Rule 6: If Age < 25 & Win-Loss < 5 & Log-in < 150 --> Discontinue."""
        elif(member[1]<25) and (member[2]<5) and (member[4]<150):
                result = member[0] + " will " + rule[14]
                memberReportFile.write(result)
                memberReportFile.write('\n')

compare()

Hello there!

Short version: I do not understand the question.

Long version:
My eagle eye detected you posting the second question about the same project.

This should be a very secret project. If you would tell us what this is about, we could provide much more help.

However your code seems to do the following:

  • You have a set of members with attributes
  • You have a set of rules. Each rule combines the member attributes into a logical statement.
  • You want to apply the rules on the members and store the result

If that is the case here, then there are far better solutions.

Just a remark:
This is a semantic error:

        rule= rulesArray[i].split(",")
        rule= rulesArray[i].split(" ")
commented: same short version +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.