file1 = open("file1.txt","r")
results = []
for line in file1:
file2 = open("file2.txt","r")
for l in file2:
if (l.split("\t")[0] == line.split("\t")[0]):
results.append(l.split("\t")[0])
break
file2.close()
file1.close()
for i in results:
print(i)
import pandas as pd
filename1 = #main file
filename2 = #the other file
main = pd.read_csv(filename1,sep='\t') # sep='\t' is for tab delimited file
side = pd.read_csv(filename2,sep='\t')
main['URN'] = main['URN'].astype(int)
side['URN'] = side['URN'].astype(int)
merge = pd.merge(main,side,on='URN',how='inner') #how=inner means the URN value is in both 2 files
#merge = merge[merge['URN'] != 62294]
print (merge)
merge.to_excel('Output.xlsx,index=False)
难吗?不,你可以很容易地
现在,这是最好的方法吗?可能不适合大文件。 (我花了74秒处理你的文件)。在
您可以查看Pandas库。它允许您将两个表作为数据帧加载,并以类似sql的方式将它们连接到所需的列上。文档应该很容易。在
尝试使用
pip install pandas
安装pandas然后运行这个:
相关问题 更多 >
编程相关推荐