I need to read a flow url as csv then this is what I do:
class Command(BaseCommand):
help = 'Admin command to import feed'
def _download_flow(self, url):
req = requests.get(url, stream=True)
if req.status_code == 200:
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
for line in req.iter_lines():
tmp.write(line)
return tmp
raise Exception('error:{}'.format(req.status_code))
def handle(self, *args, **options):
catalog = self._download_flow(options['url'])
with open(catalog.name, 'rU') as csvfile:
reader = csv.DictReader(
csvfile,
delimiter=';',
quotechar='"')
for row in reader:
raise Exception(row)
catalog.close()
Basically, from an url, I create a temporary csv file. Then, now I want to parse this file to work with lines but I don't know why my exception is not raised. (My file has content, i've checked). Do you have any clue to help me ?
Thanks
The problem came from the _download() method, the correct way to construct the file is:
def _download_flow(self, url):
req = requests.get(url, stream=True)
if req.status_code == 200:
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
for chunk in req.iter_content():
tmp.write(chunk)
return tmp
raise Exception('error:{}'.format(req.status_code))