Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用Python脚本为Excel工作表中的值形成一对多关系?_Python 3.x_Pandas_List_Python 2.7_Loops - Fatal编程技术网

Python 3.x 如何使用Python脚本为Excel工作表中的值形成一对多关系?

Python 3.x 如何使用Python脚本为Excel工作表中的值形成一对多关系?,python-3.x,pandas,list,python-2.7,loops,Python 3.x,Pandas,List,Python 2.7,Loops,我有一份Excel,其中包含以下几张表格,见附件: 现在,我想在两张纸之间生成一对多算法,这意味着对于SOURCE_KEY的每个值,都要打印RECEIVER SHEET中的所有值 指: INFORMATION SOURCE KEY 1: parent SERVICE PROVIDER KEY 1: child SERVICE PROVIDER KEY 2: child INFORMATION SOURCE KEY 2: parent SERVICE PROVIDE

我有一份Excel,其中包含以下几张表格,见附件:

现在,我想在两张纸之间生成一对多算法,这意味着对于SOURCE_KEY的每个值,都要打印RECEIVER SHEET中的所有值

指:

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  
下面是编写的代码:

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)
输出:

*********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
预期产出:

*********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
输出中大胆的错误。因此,即使P和X的比较失败,它也会进入if条件,或者my be for循环和if条件错误。

使用if P==X:而不是if[P==X]:。他们是不同的

如果[2==3]:如果[False]: 打印“通过”将打印“通过” 如果2==3:如果为假: 打印“通过”“已通过”将不会被打印 [False]对象被视为True。您可以通过执行printbool[False]来检查它,它将打印True


您可以查看关于Python的文档。

您能帮我回答这个问题吗:@HarshitKakkar我发布了一个问题的答案:
*********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