简体   繁体   中英

Downloading zip and parsing csv file in django, copying data to local DB(sqllite)

I have a url, which downloads a zip file, containing a csv file. I want to parse the data and import it to local Database(sqlite) in Django.

In brief, input = url, pre-processing download.zip-> convert to.csv, output=csv rows in DB which has columns of csv as fields.

Well, I think if you google a bit you can do it yourself. I will give you the keyword:

  • For download a file you can you use request:

import requests

url = 'https://www.facebook.com/favicon.ico' r = requests.get(url, allow_redirects=True)

open('facebook.ico', 'wb').write(r.content)

  • To parse a csv file use xlsxwriter

  • To save the data to database, I suggest you save the data to django model then call model.save()

To use django on standalone script read this

How to import Django Settings to python standalone script

To use it as a service you should use rest_framework and write a custom viewset like this. I had quite a hard time get familiar with it, so good luck, but when you get long, drf is quite handy tool for everything, just not fast.

https://medium.com/django-rest-framework/django-rest-framework-viewset-when-you-don-t-have-a-model-335a0490ba6f

You should django settings to save the path to temp file. In linux you can use /tmp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM