问题
I have an Excel having few sheets as below given in attachment as well:
sheet 1 contains :
SOURCE-KEY PAYER-NAME PAYER-CODE-TYPE PAYER-CODE
1 INDUSTRY PAY 123485
2 LEADING PAY 123422
Sheet 2 contains :
SOURCE-KEY RECIEVER-KEY RECIEVER-KEY-TYPE RECIEVER-NAME RECIEVER-CODE
1 1 PERSON CEO A222222221
1 2 PERSON CO-FOUNDER A222222221
2 3 PERSON CFO A222222221
Now I want to generate one to many algorithm between two sheets that means for every value of SOURCE_KEY print all values in RECEIVER SHEET
means :
INFORMATION SOURCE KEY 1: parent
SERVICE PROVIDER KEY 1: child
SERVICE PROVIDER KEY 2: child
INFORMATION SOURCE KEY 2: parent
SERVICE PROVIDER KEY 3: child
SERVICE PROVIDER KEY 4: child
Below is the code Written :
def loop_2100A(self,source_keys):
Information_Reciever = pd.read_excel(filename, sheet_name=1, index_col=0)
Reciever_keys=list(Information_Reciever["RECIEVER KEY"])
Source_key_in_Reciever=list(Information_Reciever.index.values)
elem =1
elems_in_both_lists = set(Source_key_in_Reciever) & set(Reciever_keys
if elem in elems_in_both_lists:
print("Value of Source key inside if ", elem)
print("Value of Reciever Key inside if", elem)
res = dict(zip(Source_key_in_Reciever, Reciever_keys))
for p in source_keys:
print("Value of P is ",p)
for x,y in res.items():
print("COMPARING of p is {} and Value of x is {} IF EQUAL GO AHEAD ".format(p,x))
if[p==x]:
print("Value of Source key passed in 2100A is", x)
print("Value of Reciever key passed to 2100B is", y)
# CALLING SOME FUNCTION TO PERFORM OPERATION ONLY WHEN P==X
else:
print("Return back to Parent tag")
return len(source_keys)
def run(self):
Read_Excel = pd.read_excel(filename, sheet_name=0,index_col=0)
source_keys = list(Read_Excel.index.values)
segs = self.loop_2100A(Parser, filename,source_keys)
OUTPUT:
*********Inside 2100A loop*********
Value of Source key inside if 1
Value of Reciever Key inside if 1
Dictionary formed is {1: 2, 2: 3}
Value of P is 1
COMPARING of p is 1 and Value of x is 1 IF EQUAL GO AHEAD
Value of Parent Source key passed in 2100A is 1
Value of Child Reciever key passed to 2100B is 2
COMPARING of p is 1 and Value of x is 2 IF EQUAL GO AHEAD
****Value of Parent Source key passed in 2100A is 2
Value of Child Reciever key passed to 2100B is 3****
Value of P is 2
COMPARING of p is 2 and Value of x is 1 IF EQUAL GO AHEAD
**Value of Parent Source key passed in 2100A is 1
Value of Child Reciever key passed to 2100B is 2**
COMPARING of p is 2 and Value of x is 2 IF EQUAL GO AHEAD
Value of Parent Source key passed in 2100A is 2
Value of Child Reciever key passed to 2100B is 3
Process finished with exit code 0
Expected Output:
*********Inside 2100A loop*********
Value of Source key inside if 1
Value of Reciever Key inside if 1
Dictionary formed is {1: 2, 2: 3}
Value of P is 1
COMPARING of p is 1 and Value of x is 1 IF EQUAL GO AHEAD
Value of Parent Source key passed in 2100A is 1
Value of Child Reciever key passed to 2100B is 2
COMPARING of p is 1 and Value of x is 2 IF EQUAL GO AHEAD
NOT EQUAL Return back to Parent tag
Value of P is 2
COMPARING of p is 2 and Value of x is 1 IF EQUAL GO AHEAD
NOT EQUAL Return back to Parent tag
COMPARING of p is 2 and Value of x is 2 IF EQUAL GO AHEAD
Value of Parent Source key passed in 2100A is 2
Value of Child Reciever key passed to 2100B is 3
Process finished with exit code 0
Bold ones in output is coming wrong. So even if the comparison of P and X fails it is going inside If condition, or my be for loop and if conditions are wrong.
回答1:
Use if p == x: instead of if [p == x]:. They are different.
if [2 == 3]: # if [False]:
print('passed'); # 'passed' will be printed
if 2 == 3: # if False:
print('passed'); # 'passed' will not be printed
The [False] object is considered True. You can check it by executing print(bool([False])), which prints True.
You can see the documentation about Truth Value Testing in Python.
来源:https://stackoverflow.com/questions/61779639/how-to-form-one-to-many-relation-for-values-in-excel-sheets-using-python-script