ddict = defaultdict(set)
file_str = query_csv_s3(s3, BUCKET_NAME, filename, sql_exp, use_header)
# read CSV to dataframe
df = pd.read_csv(StringIO(file_str))
fdf = df.drop_duplicates(subset='cleverTapId', keep='first')
fdf.dropna(inplace=True)
col_one_list = fdf['identity'].tolist()
col_two_list = fdf['cleverTapId'].tolist()
for k, v in zip(col_one_list, col_two_list):
ddict[k].add(v)
for imkey in ddict:
im_length = len(str(imkey))
if im_length == 9:
if len(ddict[imkey]) == 1:
for value in ddict[imkey]:
tdict = {imkey:value}
write_to_csv(FILE_NAME,tdict)
else:
ctlist = list(ddict[imkey])
snp_dict = {imkey:'|'.join(ctlist)}
write_to_csv(SNAP_FILE_NAME, snp_dict)
elif im_length > 0:
if len(ddict[imkey]) == 1:
for value in ddict[imkey]:
fdict = {imkey:value}
write_to_csv(FRAUD_FILE_NAME,fdict)
else:
pass
# mult_ct = list(ddict[imkey])
# mydict = {imkey:','.join(mult_ct)}
# write_to_csv(MY_FILENAME,mydict)
else:
pass
这里是write_to_csv
:
def write_to_csv(filename,mdict):
file_exists = os.path.isfile(filename)
with open(filename,'a',newline='') as csvfile:
headers = ['IM No', 'CT ID']
writer = csv.DictWriter(
csvfile,
delimiter=',',
lineterminator='\n',
fieldnames=headers
)
if not file_exists:
writer.writeheader()
for key in mdict:
writer.writerow({'IM No': key, 'CT ID': mdict[key]})
我正在使用 s3 select 读取包含 2 col 的 csv 文件。
我正在生成 1 IM :1 CTID,一对多和多对多文件并将其上传回 s3 存储桶
我该如何优化它,因为它需要 18 小时来处理从 s3 读取的 530 MB 文件大小并上传回来?