特定行我有格式的大文件(500萬行):提取從一個大文件
'User ID,Mov ID,Rating,Timestamp'
我還有一個文件(200,000線)與小得多沒有。的形式記錄:
'User ID, Mov ID'
我必須生成一個新的文件,例如,如果(用戶ID,MOV ID)從所述第二文件中的500萬行第一文件的我不應該有它匹配任何的記錄在我的新文件中。 換句話說,新文件由唯一的用戶ID,Mov ID在它沒有任何公用(用戶ID,Mov ID)和file2(200,000行)的情況下由唯一的用戶ID,Mov ID組成
我正在嘗試這種簡單的方法,花費太多時間。是否有快速的算法中實現?:
from sys import argv
import re
script, filename1, filename2 = argv
#open files
testing_small= open(filename1)
ratings=open(filename2)
##Open file to write thedata
ratings_training=open("ratings_training.csv",'w')
for line_rating in ratings:
flag=0;testing_small.seek(0)
for line_test in testing_small:
matched_line=re.match(line_test.rstrip(),line_rating)
if matched_line:
flag=1;break
if(flag==0):
ratings_training.write(line_rating)
testing_small.close()
ratings.close()
ratings_training.close()
我可以使用任何火花爲基礎的方法藏漢
您是否嘗試過熊貓/ numpy的? –
這是一個Csv文件嗎? –
是的,不,我不知道如何使用numpy –