Python皮全外接缝,不带Pandas纹

afdcj2ne  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(92)

我尝试在python中不使用panda进行完全外部连接,我已经开发了一个内部连接的代码,但无法真正编辑它以进行完全外部连接
下面是内部连接的代码

import collections
import csv
import sys

def c_merge(f1,f2):
    with open(f1,'r') as infile:
        obj=csv.reader(infile)
        header_a=next(obj)
        dict_a={row[0]: row[1:] for row in obj}

    with open(f2,'r') as infile:
        obj=csv.reader(infile)
        header_b=next(obj)
        dict_b=collections.defaultdict(list)
        for row in obj:
            dict_b[row[0]].append(row[1:])
    with open('newfile.txt','w') as newfile:
        w=csv.writer(newfile)
        w.writerow(header_a+header_b[1:])

        for m in dict_a.keys():
            for n in dict_b.get(m, [[]]):
                w.writerow([m]+dict_a[m]+n)

if __name__ == "__main__":
    c_merge(sys.argv[0],sys.argv[1])
    obj=csv.reader(open('newfile.txt','r'))
    for x in obj:
        print(x)
fnx2tebb

fnx2tebb1#

您好,欢迎来到StackOverflow!

这两部作品:

with open('newfile.txt','w') as newfile:
    w=csv.writer(newfile)
    w.writerow(header_a+header_b[1:])

    for m in set(dict_a.keys()).union(dict_b.keys()):
        for n in dict_b.get(m, [[]]):
            w.writerow([m]+dict_a.get(m, [])+n)

with open('newfile.txt','w') as newfile:
    w=csv.writer(newfile)
    w.writerow(header_a+header_b[1:])

    for m in dict_a.keys()+dict_b.keys():
        for n in dict_b.get(m, [[]]):
            w.writerow([m]+dict_a.get(m, [])+n)
完整代码:
import collections
import csv
import sys

def c_merge(f1,f2):
    with open(f1,'r') as infile:
        obj=csv.reader(infile)
        header_a=next(obj)
        dict_a={row[0]: row[1:] for row in obj}

    with open(f2,'r') as infile:
        obj=csv.reader(infile)
        header_b=next(obj)
        dict_b=collections.defaultdict(list)
        for row in obj:
            dict_b[row[0]].append(row[1:])

    with open('newfile.txt','w') as newfile:
        w=csv.writer(newfile)
        w.writerow(header_a+header_b[1:])

        for m in set(dict_a.keys()).union(dict_b.keys()):
            for n in dict_b.get(m, [[]]):
                w.writerow([m]+dict_a.get(m, [])+n)

        # OR

        """
        for m in dict_a.keys()+dict_b.keys():
            for n in dict_b.get(m, [[]]):
                w.writerow([m]+dict_a.get(m, [])+n)
        """

if __name__ == "__main__":
    c_merge(sys.argv[0],sys.argv[1])
    obj=csv.reader(open('newfile.txt','r'))
    for x in obj:
        print(x)

相关问题