Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
Create and read a Temporary file in a Django Command

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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda