Python Queries, CSV, Sha256 and BOM
I am building a CSV set for athletes using queries and Python 2.7.
These files are generated by MSFT Report Server and go through iso-8859-1, the requests say.
Because I deal with thousands every night, I want to go through the files and compare to the previous hash for the athlete. If the hash matches, I won't bother saving the file to disk. These files are small - about 6K at most - so there is no interference / streaming issue.
sha256 fails, however, due to the annoying spec with these files. I've looked at 10 different "solutions" here and can't find one that pulls the BOM through decode.encode so that I can do my sha256.
The workaround that I might have to go back to is that I can write the file to disk and then sha256 it there. But that seems like a very bad form.
If I can remove the BOM at the beginning, I have a process that works with sha256 and saves me from working with unnecessary files.
I think the problem might be that I'm supposedly trying to do string operations on a file. But since the object is still a hex stream u / ..., I thought these operators would work ...
Here are the details:
'http://22.214.171.124/ReportServer?%2fCPTC%2fWomens1stHalfDetail&Team=&player=17424&rs:Format=CSV') r.status_code 200 r.raw <requests.packages.urllib3.response.HTTPResponse object at 0x18afb70> r.encoding 'ISO-8859-1' print r.headers['content-type'] text/plain r.text u'\xff'r = requests.get(
The first attempt to convert cannot be decoded using the specified encoding type!
'iso-8859-1').encode('utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)> z = r.text > z.decode(
And in fact the "type" of z is now different from what was expected, perhaps because of sys (mac; utf8)?
'unicode'> z u'\xff' z[0:5] u'\xff\xfem\x00a'type(z) <type
Various attempts at decoding and encoding have not let me down; here is one of many such attempts.
'utf-8-sig').encode('utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/encodings/utf_8_sig.py", line 22, in decode (output, consumed) = codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)> z.decode(
I'm pretty sure the answer is one line; I just don't see it. Any recommendations that are most appreciated.
source to share