从CSV源创建django模型

时间:2017-11-15 11:12:17

标签: python django pandas google-adwords

由于某种原因,AdWords没有正常的REST API,而是使用SOAP api,它返回.csv文件或最近返回一个字符串。

我正在使用django,我需要将结果保存在我的数据库中。目前我使用Pandas将字符串转换为数据帧,然后我遍历它并将其保存在我的数据库中。然而,这个过程缓慢而且效率低下。我不想仅仅为此使用熊猫。 AST会在我的情况下工作吗?

最快的方法是什么?

我的pandas代码如下:

abc = report_downloader.DownloadReportAsString(report, skip_report_header=True, skip_column_header=False,skip_report_summary=True)
print(abc)
print(type(abc))
df = pd.read_csv(StringIO(abc), sep=",",)
#print (df.to_string())

for index, row in df.iterrows():

    ctr = float(float(row['CTR'].strip('%')) / 100)
    conv_rate = float(float(row['Conv. rate'].strip('%')) / 100)
    cost = row['Cost']/1000000
    cpm = row['Avg. CPM']/1000000
    avg_cpc = row['Avg. CPC']/1000000
    def_data = {
                'impressions':row['Impressions'],
                'ctr':ctr,
                'avg_cpc':avg_cpc,
                'conversion_rate':conv_rate,
                'clicks':row['Clicks'],
                'cost':cost,
                'cpm':cpm,
                'conversions':row['Conversions'],
                }


    obj, created = AdWordsData.objects.get_or_create(
                                                    user=user,
                                                    date=row['Day'],
                                                    campaign=row['Campaign'],
                                                    ad_group=row['Ad group'],
                                                    final_url=row['Final URL'],
                                                    tracking_template=row['Tracking template'],
                                                    defaults=def_data,
                                                    )

    if not created:
        obj.__dict__.update(**def_data)
        obj.save()

else:
    print ('No campaigns were found.')

ABC(adwords的数据返回是一个字符串,如下所示:

Day,Campaign,Ad group,Final URL,Tracking template,Impressions,CTR,Avg. CPC,Conv. rate,Clicks,Cost,Avg. CPM,Conversions
2017-09-08,mysite.me Many Keywords,Many Keywords,https://mysite.me, --,364,0.82%,73333,0.00%,3,220000,604396,0.00
2017-09-10,mysite.me Many Keywords,Many Keywords,https://mysite.me, --,1562,3.46%,435926,0.00%,54,23540000,15070423,0.00
2017-09-11,mysite.me Many Keywords,Many Keywords,https://mysite.me, --,1806,3.49%,387619,0.00%,63,24420000,13521595,0.00

1 个答案:

答案 0 :(得分:0)

使用pandas加载csv确实有点过头了。 您可以使用basic python迭代csv行。

或者更好的是,您可以使用现有的django库,它允许您从csv文件/输入创建django模型列表。例如:django import-export

>>> import tablib
>>> from import_export import resources
>>> from core.models import Book
>>> book_resource = resources.modelresource_factory(model=Book)()
>>> dataset = tablib.Dataset(['', 'New book'], headers=['id', 'name'])
>>> result = book_resource.import_data(dataset, dry_run=True)
>>> print result.has_errors()
False
>>> result = book_resource.import_data(dataset, dry_run=False)