在Django命令中创建和读取临时文件

时间:2017-04-27 16:22:42

标签: python django temporary-files

我需要将流式网址读取为csv然后这就是我所做的:

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()

基本上,我从一个url创建一个临时的csv文件。然后,现在我想解析这个文件以使用行,但我不知道为什么我的异常没有引发。 (我的文件有内容,我已经检查过了)。 你有什么线索可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

问题来自_download()方法,构造文件的正确方法是:

#include "SDL.h"
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h> 


typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;

// Temp?
#define internal static
#define local_persist static
#define global_variable static
global_variable SDL_Texture *Texture;
global_variable void *BitmapMemory;  //Pixels
global_variable int BitmapWidth;     //Texture Width
global_variable int BitmapHeight;     //Texture Width
global_variable int BytesPerPixel = 4;

bool HandleEvent(SDL_Event*);
// Backbuffer Functions.
internal void SDLResizeTexture(SDL_Renderer*, int, int);
internal void SDLUpdateWindow(SDL_Window*, SDL_Renderer*);
internal void RenderGradient(int, int);

int main(int argc, char *argv[]){

    SDL_Init(SDL_INIT_VIDEO);
    // Create Window.
    SDL_Window *Window = SDL_CreateWindow("The Origin Of Evil",
                                          SDL_WINDOWPOS_UNDEFINED, 
                                          SDL_WINDOWPOS_UNDEFINED, 
                                          640, 480, 
                                          SDL_WINDOW_RESIZABLE);
    if(Window){
        // Create Renderer.
        SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, 0);
        if(Renderer){
            bool Running = true;
            int Width, Height;
            SDL_GetWindowSize(Window, &Width, &Height);
            SDLResizeTexture(Renderer, Width, Height);
            int XOffset = 0;
            int YOffset = 0;
            while(Running){
                SDL_Event Event;
                while(SDL_PollEvent(&Event)){
                    if(HandleEvent(&Event)){
                        Running = false;
                    }
                }
                RenderGradient(XOffset, YOffset);
                SDLUpdateWindow(Window, Renderer);

                ++XOffset;
                YOffset += 2;
            }
        }else{
            //TODO: Logging
            printf("No Renderer.\n");
        }
    }else{
        //TODO: Logging
        printf("No Window.\n");
    }

    SDL_Quit(); 
    return 0;
}

// True if we want to quit the program.
bool HandleEvent(SDL_Event *Event){
    bool ShouldQuit = false;
    switch (Event->type) {
        case SDL_QUIT:
        {
            printf("SDL_Quit\n") ;
            ShouldQuit = true;
        }break;

        case SDL_WINDOWEVENT:
        {
            switch (Event->window.event) {
                case SDL_WINDOWEVENT_SIZE_CHANGED:
                {
                    printf("SDL_WINDOWEVENT_SIZE_CHANGED\n");
                    SDL_Window *Window = SDL_GetWindowFromID(Event->window.windowID);
                    SDL_Renderer * Renderer = SDL_GetRenderer(Window);
                    SDLResizeTexture(Renderer, Event->window.data1, Event->window.data2);
                }break;

                case SDL_WINDOWEVENT_FOCUS_GAINED:
                {
                    printf("SDL_WINDOWEVENT_FOCUS_GAINED\n");
                }break;

                case SDL_WINDOWEVENT_EXPOSED:
                {
                    SDL_Window *Window = SDL_GetWindowFromID(Event->window.windowID);
                    SDL_Renderer *Renderer = SDL_GetRenderer(Window);
                    SDLUpdateWindow(Window, Renderer);
                }break;
            }

        }break;
    }
    return(ShouldQuit);
}

internal void SDLResizeTexture(SDL_Renderer *Renderer, int Width, int Height){
    if(BitmapMemory){
        munmap(BitmapMemory, BitmapWidth * BitmapHeight * BytesPerPixel);
    }
    if(Texture){
        SDL_DestroyTexture(Texture);
    }
    Texture = SDL_CreateTexture(Renderer, SDL_PIXELFORMAT_ARGB8888,
                                 SDL_TEXTUREACCESS_STREAMING,
                                 Width,
                                 Height);
    BitmapWidth = Width;
    BitmapHeight = Height;
    BitmapMemory = mmap(0,
                   Width * Height * BytesPerPixel,
                   PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_PRIVATE,
                   -1,
                   0);
}

internal void SDLUpdateWindow(SDL_Window *Window, SDL_Renderer *Renderer){
    SDL_UpdateTexture(Texture, 0, BitmapMemory, BitmapWidth * BytesPerPixel);
    SDL_RenderCopy(Renderer, Texture, 0, 0);
    SDL_RenderPresent(Renderer);
}

internal void RenderGradient(int BlueOffset, int GreenOffset){
    int Width = BitmapWidth;
    int Height = BitmapHeight;
    int Pitch = Width * BytesPerPixel;
    uint8 *Row = (uint8 *)BitmapMemory;
    for(int Y = 0; Y > BitmapHeight; ++Y){
        uint32 *Pixel = (uint32 *)Row;
        for (int X = 0; X < BitmapWidth; ++X) {
           uint8 Blue = (X + BlueOffset);
           uint8 Green = (Y + GreenOffset);
           *Pixel++ = ((Green << 8) | Blue);
        }
        Row += Pitch;
    }
}