私は2つのファイルを持っています:
- metadata.csv: ID が含まれ、その後にベンダー名、ファイル名などが続きます。
- hashes.csv: ID が含まれ、その後にハッシュが続きます。ID は基本的に、ファイル メタデータをそのハッシュに関連付ける一種の外部キーです。
特定のベンダーに関連するすべてのハッシュをすばやく抽出するために、このスクリプトを作成しました。hashes.csv の処理が完了する前にクラップアウトします
stored_ids = []
# this file is about 1 MB
entries = csv.reader(open(options.entries, "rb"))
for row in entries:
# row[2] is the vendor
if row[2] == options.vendor:
# row[0] is the ID
stored_ids.append(row[0])
# this file is 1 GB
hashes = open(options.hashes, "rb")
# I iteratively read the file here,
# just in case the csv module doesn't do this.
for line in hashes:
# not sure if stored_ids contains strings or ints here...
# this probably isn't the problem though
if line.split(",")[0] in stored_ids:
# if its one of the IDs we're looking for, print the file and hash to STDOUT
print "%s,%s" % (line.split(",")[2], line.split(",")[4])
hashes.close()
このスクリプトは、停止する前に hashes.csv から約 2000 のエントリを取得します。私は何を間違っていますか?行ごとに処理していると思いました。
ps。csv ファイルは一般的な HashKeeper 形式で、解析しているファイルは NSRL ハッシュ セットです。http://www.nsrl.nist.gov/Downloads.htm#converter
更新:以下の実用的なソリューション。コメントしてくれたみんなありがとう!
entries = csv.reader(open(options.entries, "rb"))
stored_ids = dict((row[0],1) for row in entries if row[2] == options.vendor)
hashes = csv.reader(open(options.hashes, "rb"))
matches = dict((row[2], row[4]) for row in hashes if row[0] in stored_ids)
for k, v in matches.iteritems():
print "%s,%s" % (k, v)