Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'broken': 0.04; 'resulting': 0.04; 'anyway.': 0.05; 'encoding': 0.05; 'output': 0.05; 'binary': 0.07; 'context': 0.07; 'failing': 0.07; 'string': 0.09; 'assuming': 0.09; 'badly': 0.09; 'encode': 0.09; 'function,': 0.09; 'pyodbc': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '0-127': 0.16; 'carefully.': 0.16; 'codec': 0.16; 'hint': 0.16; 'inserting': 0.16; 'jacob': 0.16; 'ordinal': 0.16; 'range,': 0.16; 'range.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'redundant': 0.16; 'subject:String': 0.16; 'subject:format': 0.16; 'subject:type': 0.16; 'subject:when': 0.16; 'traceback.': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'thoughts': 0.19; 'version.': 0.19; 'fit': 0.20; 'seems': 0.21; '>>>': 0.22; 'preferred': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'module,': 0.24; 'unicode': 0.24; 'sort': 0.25; 'script': 0.25; 'posts': 0.26; 'skip:" 40': 0.26; 'pass': 0.26; 'subject:/': 0.26; 'skip:" 20': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'character': 0.29; 'thus': 0.29; 'characters': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; '"",': 0.31; 'strip': 0.31; 'file': 0.32; 'probably': 0.32; 'run': 0.32; 'another': 0.32; '(most': 0.33; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'basic': 0.35; "can't": 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'convert': 0.35; 'hundreds': 0.35; 'really': 0.36; 'subject:data': 0.36; 'subject:one': 0.36; 'example,': 0.37; 'feed': 0.38; 'generic': 0.38; 'handle': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'issue': 0.38; 'fact': 0.38; 'recent': 0.39; 'expect': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'documents,': 0.60; 'failures': 0.60; 'skip:c 50': 0.60; 'most': 0.60; 'tell': 0.60; 'mentioned': 0.61; 'range': 0.61; 'show': 0.63; 'our': 0.64; 'within': 0.65; 'side': 0.67; 'fields,': 0.84; 'step.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: String character encoding when converting data from one type/format to another Date: Wed, 07 Jan 2015 13:11:48 +0100 Organization: None References: <9A569F491BCD444E9512731789AC7853@JakesPC> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd93e5.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420632947 news.xs4all.nl 2940 [2001:888:2000:d::a6]:43192 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83288 Jacob Kruger wrote: > I'm busy using something like pyodbc to pull data out of MS access .mdb > files, and then generate .sql script files to execute against MySQL > databases using MySQLdb module, but, issue is forms of characters in > string values that don't fit inside the 0-127 range - current one seems to > be something like \xa3, and if I pass it through ord() function, it comes > out as character number 163. > > Now issue is, yes, could just run through the hundreds of thousands of > characters in these resulting strings, and strip out any that are not > within the basic 0-127 range, but, that could result in corrupting data - > think so anyway. > > Anyway, issue is, for example, if I try something like > str('\xa3').encode('utf-8') or str('\xa3').encode('ascii'), or "\xa3" already is a str; str("\xa3") is as redundant as str(str(str("\xa3"))) ;) > str('\xa3').encode('latin7') - that last one is actually our preferred > encoding for the MySQL database - they all just tell me they can't work > with a character out of range. encode() goes from unicode to byte; you want to convert bytes to unicode and thus need decode(). In this context it is important that you tell us the Python version. In Python 2 str.encode(encoding) is basically str.decode("ascii").encode(encoding) which is why you probably got a UnicodeDecodeError in the traceback: >>> "\xa3".encode("latin7") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/encodings/iso8859_13.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_table) UnicodeDecodeError: 'ascii' codec can't decode byte 0xa3 in position 0: ordinal not in range(128) >>> "\xa3".decode("latin7") u'\xa3' >>> print "\xa3".decode("latin7") £ Aside: always include the traceback in your posts -- and always read it carefully. The fact that "latin7" is not mentioned might have given you a hint that the problem was not what you thought it was. > Any thoughts on a sort of generic method/means to handle any/all > characters that might be out of range when having pulled them out of > something like these MS access databases? Assuming the data in Access is not broken and that you know the encoding decode() will work. > Another side note is for binary values that might store binary values, I > use something like the following to generate hex-based strings that work > alright when then inserting said same binary values into longblob fields, > but, don't think this would really help for what are really just most > likely badly chosen copy/pasted strings from documents, with strange > encoding, or something: > #sample code line for binary encoding into string output > s_values += "0x" + str(l_data[J][I]).encode("hex").replace("\\", "\\\\") + > ", " I would expect that you can feed bytestrings directly into blobs, without any preparatory step. Try it, and if you get failures show us the failing code and the corresponding traceback.