Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Saran Ahluwalia Newsgroups: comp.lang.python Subject: Re: Understanding how to quote XML string in order to serialize using Python's ElementTree Date: Sat, 9 Jan 2016 18:15:03 -0500 Lines: 155 Message-ID: References: <5691887A.1080203@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ovNKSLOMwOMkqi+uQUysfQdao6tUsoJqwLppsWljdwhQ== 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; 'handler': 0.04; 'root': 0.04; 'subject:Python': 0.05; 'sys': 0.05; '"__main__":': 0.07; '-*-': 0.07; '__name__': 0.07; 'column': 0.07; 'data):': 0.07; 'data:': 0.07; 'formatting': 0.07; 'sanity': 0.07; 'utf-8': 0.07; 'cc:addr:python-list': 0.09; 'coding:': 0.09; 'csv': 0.09; 'delimiter': 0.09; 'ide': 0.09; 'name):': 0.09; 'os.path': 0.09; 'reformat': 0.09; 'script,': 0.09; 'stringio': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'utilizing': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; "skip:' 30": 0.15; 'subject: \n ': 0.15; '\'"\'': 0.16; "','": 0.16; '(line': 0.16; '2016': 0.16; 'cc:name:python': 0.16; 'from:addr:ahlusar.ahluwalia': 0.16; 'from:name:saran ahluwalia': 0.16; 'guidance.': 0.16; 'ideally,': 0.16; 'idx': 0.16; 'idx,': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'self)': 0.16; 'skip:r 130': 0.16; 'subject:XML': 0.16; 'syntaxerror:': 0.16; 'variations': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.16; 'string': 0.17; 'element': 0.18; 'try:': 0.18; '>': 0.18; '(in': 0.18; 'email addr:gmail.com>': 0.18; 'variable': 0.18; 'windows': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'so.': 0.22; '"",': 0.22; 'assign': 0.22; 'rid': 0.22; 'pass': 0.22; 'sat,': 0.23; 'this:': 0.23; 'tried': 0.24; 'import': 0.24; 'xml': 0.24; 'header :In-Reply-To:1': 0.24; '(which': 0.26; 'skip:" 20': 0.26; 'skip:_ 20': 0.26; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:e 30': 0.27; 'yield': 0.27; 'this.': 0.28; "skip:' 10": 0.28; 'quoting': 0.29; 'skip:/ 80': 0.29; 'skip:2 30': 0.29; 'skip:q 20': 0.29; 'skip:r 50': 0.29; 'skip:r 60': 0.29; 'whitespace': 0.29; 'excel': 0.29; 'raise': 0.29; 'print': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'skip:/ 20': 0.33; 'file': 0.34; 'except': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'attempt': 0.35; 'false': 0.35; 'follows:': 0.35; 'unknown': 0.35; 'received:74.125.82': 0.35; 'should': 0.36; 'there': 0.36; 'skip:m 40': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'method': 0.37; 'beyond': 0.37; 'feedback': 0.38; 'thank': 0.38; 'means': 0.39; 'data': 0.39; 'skip:e 20': 0.39; 'skip:x 10': 0.40; 'your': 0.60; 'address': 0.61; 'information': 0.63; 'due': 0.65; 'believe': 0.66; 'here': 0.66; 'skip:\xc2 10': 0.67; 'receive': 0.71; 'sans': 0.72; '0000': 0.84; 'dialect': 0.84; 'received:74.125.82.41': 0.84; 'specs:': 0.84; 'vera': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=eVozdLBrrMVRrHpzI3x6VHuBKmuPJC0tJtUu3ZkjLuM=; b=RBDYRhyqbUsqJliW38fJiun48l19epGWDE+Hqa5EZHqUPB/ZCs//uRDJ9i2zK+/gs1 ifeLW/L4U2R4MYzQmrWlp+r/cNAGJ0kUt94bXg5A6lBy5TAbnOnizJ82uoHv+06YCsI7 9Fkppju9CJrqvFw1o2Q42zatgPX6IharhaGu5kk0lRVkD83zM0bem/RFpe4GaXouUwb5 mxq4YOz3bmsG4CRg2rJHPHQulPN3nKSREXyQpvylJAp7Imn0crXoj0ubAycreIkTW+Co 5jhzVQYzxAufEsAlwydqN6NYiVe10vXrDtSoST3KwgjICOGPctoOR0jg3ByU1/2bvSoX LZGw== X-Received: by 10.28.47.213 with SMTP id v204mr5462191wmv.69.1452381323104; Sat, 09 Jan 2016 15:15:23 -0800 (PST) In-Reply-To: <5691887A.1080203@gmail.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101419 Thank you for the feedback on this. I believe that the excel dialect includes just that: class excel(Dialect): delimiter = ',' quotechar = '"' doublequote = True skipinitialspace = False lineterminator = '\r\n' quoting = QUOTE_MINIMAL On Sat, Jan 9, 2016 at 5:23 PM, Karim wrote: > > > On 09/01/2016 21:54, kbtyo wrote: > >> My specs: >> >> Python 3.4.3 >> Windows 7 >> IDE is Jupyter Notebooks >> >> What I have referenced: >> >> 1) >> http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml >> >> 2) >> >> http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes >> >> 3) >> http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python >> >> >> Here is the data (in CSV format) and script, respectively, (I have tried >> variations on serializing Column 'E' using both Sax and ElementTree): >> >> i) >> >> A,B,C,D,E,F,G,H,I,J >> "3","8","1","> />2312> />285SChecking10","> TransactionID="2" >> RequestType="HoldInquiry">0000',0001,0070,","1967-12-25 >> 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0" >> >> ii) >> >> #!/usr/bin/python >> # -*- coding: utf-8 -*- >> import os.path >> import sys >> import csv >> from io import StringIO >> import xml.etree.cElementTree as ElementTree >> from xml.etree.ElementTree import XMLParser >> import xml >> import xml.sax >> from xml.sax import ContentHandler >> >> class MyHandler(xml.sax.handler.ContentHandler): >> def __init__(self): >> self._charBuffer = [] >> self._result = [] >> >> def _getCharacterData(self): >> data = ''.join(self._charBuffer).strip() >> self._charBuffer = [] >> return data.strip() #remove strip() if whitespace is important >> >> def parse(self, f): >> xml.sax.parse(f, self) >> return self._result >> >> def characters(self, data): >> self._charBuffer.append(data) >> >> def startElement(self, name, attrs): >> if name == 'Response': >> self._result.append({}) >> >> def endElement(self, name): >> if not name == 'Response': self._result[-1][name] = >> self._getCharacterData() >> >> def read_data(path): >> with open(path, 'rU', encoding='utf-8') as data: >> reader = csv.DictReader(data, delimiter =',', quotechar="'", >> skipinitialspace=True) >> for row in reader: >> yield row >> >> if __name__ == "__main__": >> empty = '' >> Response = 'sample.csv' >> for idx, row in enumerate(read_data(Response)): >> if idx > 10: break >> data = row['E'] >> print(data) # The before >> data = data[1:-1] >> data = ""'{}'"".format(data) >> print(data) # Sanity check >> # data = '> RequestType="HoldInquiry">0000',0001,0070,' >> try: >> root = ElementTree.XML(data) >> # print(root) >> except StopIteration: >> raise >> pass >> # xmlstring = StringIO(data) >> # print(xmlstring) >> # Handler = MyHandler().parse(xmlstring) >> >> >> Specifically, due to the quoting in the CSV file (which is beyond my >> control), I have had to resort to slicing the string (line 51) and then >> formatting it (line 52). >> >> However the print out from the above attempt is as follows: >> >> "0000' >> 0000 >> >> File "", line unknown >> ParseError: no element found: line 1, column 69 >> Interestingly - if I assign the variable "data" (as in line 54) I receive >> this: >> >> File "", line 56 >> data = '> RequestType="HoldInquiry">0000',0001,0070,' >> ^ >> SyntaxError: invalid token >> >> I seek feedback and information on how to address utilizing the most >> Pythonic means to do so. Ideally, is there a method that can leverage >> ElementTree. Thank you, in advance, for your feedback and guidance. >> > > In fact to get rid of double quote simply create your csv reader like > that: > > reader = csv.DictReader(data, dialect='excel', skipinitialspace=True) > > You should then don't need to slice data variable and reformat it. > > Karim > > >