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()