using python 3.5 saving csv from url drops CR and LF
Question: I'm using Python 3.5.0 to grab some census data. When I use my script it does
retrieve the data from the url and saves it but the file that was saved can't
be imported to SQL because it somehow dropped the {CR}{LF}. How can I get the
file it saves able of being imported to SQL?

    
    
    try:
        url = 'https://www.census.gov/popest/data/counties/asrh/2014/files/CC-EST2014-ALLDATA.csv'
    
        headers = {}
        headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
    
        req = urllib.request.Request(url,headers=headers)
        resp = urllib.request.urlopen(req)
        respData = resp.read()
    
        saveFile = open('Vintage2014.csv' ,'w')
        saveFile.write(str(respData))
        saveFile.close()
    
    except Exception as e:
        print(str(e))
    


Answer: Note, the file you are trying to download does not contain `CRLF` only `LF`.

You could use the following approach to convert the bytes to a suitable
string. This should also result in you getting `CRLF`:

    
    
    import urllib.request
    
    try:
        url = 'https://www.census.gov/popest/data/counties/asrh/2014/files/CC-EST2014-ALLDATA.csv'
        headers = {}
        headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
        req = urllib.request.Request(url, headers=headers)
        resp = urllib.request.urlopen(req)
        respData = resp.read()
    
        with open('Vintage2014.csv', 'w') as saveFile:
            saveFile.write(respData.decode('latin-1'))
    
    except Exception as e:
        print(str(e))
    

