我尝试在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)
1条答案
按热度按时间fnx2tebb1#
您好,欢迎来到StackOverflow!
这两部作品:
或
完整代码: