在 python 中处理几个大的 csv
processing several big csv in python
我正在处理两个大数据集(一个有 100 万条记录和 6 个特征,另一个有大约 7000 个记录和 9 个特征)。我已经使用字典读取数据,没问题,但主要思想是对于(1000000 条记录)中的每条记录,我需要从另一个 csv 文件中找到匹配的记录...(包括几个 if..else 语句)..每次我都应该 return 到文件的开头来搜索匹配的规则。
这花了几天时间,仅在 5 天后仍处理了大约 200 000..
with open(sys.argv[1]) as csvfile:
reader1 = csv.DictReader(csvfile)
for row in reader1:
Find_Match(row)
def Find_Match(row):
matches = []
reader2 = csv.DictReader(open(sys.argv[2]))
for k in reader2:
if (several kind of statement for matching like is row[0] is eaual to k[0] and row[1] is in k[1]..and so on)
else:
print "no match"
matches.append(row, k)
如果在 运行 内存中的所有内容之后它仍然很慢(正如@Alexander 所建议的)。另一个想法,但取决于数据:也许你可以尝试提前退出第二个循环,例如在其中一列中创建值的 Set/Dictionary,例如:
col0 = {k[0] for k in dataset2}
def Find_Match(row):
for k in dataset2:
if not row[0] in col0:
continue
#...
我正在处理两个大数据集(一个有 100 万条记录和 6 个特征,另一个有大约 7000 个记录和 9 个特征)。我已经使用字典读取数据,没问题,但主要思想是对于(1000000 条记录)中的每条记录,我需要从另一个 csv 文件中找到匹配的记录...(包括几个 if..else 语句)..每次我都应该 return 到文件的开头来搜索匹配的规则。 这花了几天时间,仅在 5 天后仍处理了大约 200 000..
with open(sys.argv[1]) as csvfile:
reader1 = csv.DictReader(csvfile)
for row in reader1:
Find_Match(row)
def Find_Match(row):
matches = []
reader2 = csv.DictReader(open(sys.argv[2]))
for k in reader2:
if (several kind of statement for matching like is row[0] is eaual to k[0] and row[1] is in k[1]..and so on)
else:
print "no match"
matches.append(row, k)
如果在 运行 内存中的所有内容之后它仍然很慢(正如@Alexander 所建议的)。另一个想法,但取决于数据:也许你可以尝试提前退出第二个循环,例如在其中一列中创建值的 Set/Dictionary,例如:
col0 = {k[0] for k in dataset2}
def Find_Match(row):
for k in dataset2:
if not row[0] in col0:
continue
#...