Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85387

Re: Python 3.x stuffing utf-8 into SQLite db

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mmr15@case.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'encoding': 0.05; 'subject:Python': 0.06; '"""': 0.07; 'args': 0.07; 'mentioned,': 0.07; 'python3': 0.07; 'utf-8': 0.07; '*is*': 0.09; '__name__': 0.09; 'subject:into': 0.09; 'python': 0.11; 'def': 0.12; 'windows': 0.15; "'__main__':": 0.16; "'rb')": 0.16; 'csv': 0.16; 'editor,': 0.16; 'hex': 0.16; 'implies': 0.16; 'mode,': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'file,': 0.19; 'input': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'skip': 0.24; 'excel': 0.26; 'certain': 0.27; 'defined': 0.27; 'header:In-Reply- To:1': 0.27; 'to:2**1': 0.27; 'point': 0.28; 'chris': 0.29; 'character': 0.29; 'wonder': 0.29; "i'm": 0.30; 'lines': 0.31; '(unless': 0.31; 'linux.': 0.31; 'file': 0.32; 'text': 0.33; 'skip:# 10': 0.33; 'skip:b 30': 0.33; 'could': 0.34; 'received:209.85': 0.35; 'created': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'received:209': 0.37; 'problems': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'read': 0.60; 'blank': 0.60; 'most': 0.60; 'entire': 0.61; "you're": 0.61; 'save': 0.62; 'telling': 0.64; 'occur': 0.65; 'to:addr:gmail.com': 0.65; '8bit%:31': 0.68; 'export': 0.74; '3.4': 0.84; 'spreadsheet': 0.84; 'quotation': 0.93; '2013': 0.98
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=DCfgCXjjtAWFGlVCcD9VTQZoppXjbrsrZ9AzOes9av8=; b=QTJNZLaqACxDLEuvprvjNFnsb2G+Ftvl0NV2K52wsDQjmv/gj1QmsFfUdkX4lijkTv 30nsS3FWjK+8dLI/g7rVJ6lXswYvEq0d+0E2OlrckaDRVLfcZvc+K5kPZAa4z1jbCnex 8EKD/l6Ruq7CvOlYSNkfuIIJdmRlWPf33MkwSrf4W1su2oph56hSHXD2r2xE9AlDkCpf R5H+lenYWheS7dAHn9Uh3CW32iITONZYp+1PZfYJAeRre9llbkpEtJQTLt6jKo6LIy7/ U62GEdcd96Sg3k++3+SjvwxHVcRHaD9vwQSk+Woug6moBvNt9iPsqh4LxJLokLOGY5km 2u5g==
X-Gm-Message-State ALoCoQkcEU1ewXImO1fxarTE8VLpJvq23McCsIPnMGsGY4IE0TtFwVlJVS3eRAeU78L5iuygZKZnWh8A44XZtbcg4/H6mm7AOu3rGUlOUDnPGweLHQgE0B6ZEQBgZ1TIDAnnd18JW7uU
X-Received by 10.107.149.203 with SMTP id x194mr15477024iod.12.1423504442412; Mon, 09 Feb 2015 09:54:02 -0800 (PST)
X-Received by 10.107.149.203 with SMTP id x194mr15477011iod.12.1423504442330; Mon, 09 Feb 2015 09:54:02 -0800 (PST)
From Matthew Ruffalo <mmr15@case.edu>
X-Google-Original-From Matthew Ruffalo <matthew.ruffalo@case.edu>
Date Mon, 09 Feb 2015 12:54:00 -0500
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
MIME-Version 1.0
To Skip Montanaro <skip.montanaro@gmail.com>, Python <python-list@python.org>
Subject Re: Python 3.x stuffing utf-8 into SQLite db
References <CANc-5UwYdoPLKuqRbrfbOrQTVafOaZmWZ+8SnDc-TZp1NEXcQg@mail.gmail.com> <CAPTjJmpJV4KYcL50_=0hv+BLKaGX_X9wqQgMJeszMrHgAK+drA@mail.gmail.com> <CANc-5Uwb51DKan1xgqwWDT2WFAZk-_NcqgLcnQfzYHzQzps3xA@mail.gmail.com>
In-Reply-To <CANc-5Uwb51DKan1xgqwWDT2WFAZk-_NcqgLcnQfzYHzQzps3xA@mail.gmail.com>
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Junkmail-Whitelist YES (by domain whitelist at mpv1.tis.cwru.edu)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18571.1423504827.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423504827 news.xs4all.nl 2869 [2001:888:2000:d::a6]:53861
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85387

Show key headers only | View raw


On 02/09/2015 12:30 PM, Skip Montanaro wrote:
> Thanks, Chris. Are you telling me I should have defined the input file
> encoding for my CSV file as CP-1252, or that something got hosed on
> the export from XLSX to CSV? Or something else?
>
> Skip

Hi Skip-

I think it's most likely that the encoding issues happened in the export
from XLSX to CSV (unless the data is malformed in the original XLSX
file, of course). The file you're reading *is* valid UTF-8, and you're
reading it in text mode, so seeing '’' in certain lines implies that
the files might contain '’'.encode('utf-8') ==
b'\xc3\xa2\xe2\x82\xac\xe2\x84\xa2'. You could verify this with a hex
editor, or use something like the following:

"""
#!/usr/bin/env python3
from argparse import ArgumentParser
                                                                                                                       

def
dump_lines_with_high_bytes(filename):                                                                              

    with open(filename, 'rb') as
f:                                                                                    

        for line in
f:                                                                                                 

            if any(byte >= 0x80 for byte in
line):                                                                     
               
print(line)                                                                                            


if __name__ == '__main__':
    p = ArgumentParser()
    p.add_argument('filename')
    args = p.parse_args()
    dump_lines_with_high_bytes(args.filename)
"""

I'm a bit surprised that LibreOffice would mangle the encoding like this
-- the entire point of using a format like XLSX is to avoid encoding
issues. I just created a blank spreadsheet with Excel 2013 on Windows 7,
pasted a U+2019 RIGHT SINGLE QUOTATION MARK into the top-left cell,
saved it to a .xlsx file, and opened it with LibreOffice Calc 4.2.7.2 on
Linux. The quot character displayed correctly, and I was able to save it
to valid UTF-8 CSV that I could read with Python 3.4 without any mojibake.

I wonder whether the encoding problems happened with whatever data was
used to create your .xlsx file -- as Chris mentioned, this would occur
if UTF-8 bytes are incorrectly decoded as Windows cp1252.

MMR...

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 3.x stuffing utf-8 into SQLite db Matthew Ruffalo <mmr15@case.edu> - 2015-02-09 12:54 -0500

csiph-web