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


Groups > comp.lang.python > #101441

Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Saran Ahluwalia <ahlusar.ahluwalia@gmail.com>
Newsgroups comp.lang.python
Subject Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"
Date Sun, 10 Jan 2016 12:53:28 -0500
Lines 261
Message-ID <mailman.6.1452448435.3151.python-list@python.org> (permalink)
References <876e5e0c-42c4-416a-90c0-ac2641e81949@googlegroups.com> <56929498$0$1622$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de 05YsXEnOjK05bzxjPQp8Ag+s6WUi/P5YQyZB9j3Gt+ow==
Return-Path <ahlusar.ahluwalia@gmail.com>
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; 'else:': 0.03; 'elif': 0.04; 'root': 0.04; 'true,': 0.04; 'none:': 0.05; 'sys': 0.05; '__name__': 0.07; 'column': 0.07; 'data:': 0.07; 'differently': 0.07; 'emulate': 0.07; 'none)': 0.07; 'prefix': 0.07; 'pretend': 0.07; 'skip:/ 10': 0.07; "subject:' ": 0.07; 'cc:addr:python- list': 0.09; 'csv': 0.09; 'dict': 0.09; 'err:': 0.09; 'fails.': 0.09; 'nameerror:': 0.09; 'os.path': 0.09; 'posted.': 0.09; 'rows': 0.09; 'stringio': 0.09; 'python': 0.10; 'assume': 0.11; 'jan': 0.11; 'subject:not': 0.11; 'exception': 0.13; 'def': 0.13; 'suggest': 0.15; '(but': 0.15; 'applies': 0.15; 'java,': 0.15; 'subject: \n ': 0.15; "'__main__':": 0.16; '(apologies': 0.16; '1):': 0.16; '2016': 0.16; 'attributes,': 0.16; 'cc:name:python': 0.16; 'different,': 0.16; 'element:': 0.16; 'eprefix': 0.16; 'exception;': 0.16; 'files...': 0.16; 'from:addr:ahlusar.ahluwalia': 0.16; 'from:name:saran ahluwalia': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'script?': 0.16; 'set()': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'used:': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'debugging': 0.18; 'element': 0.18; 'script.': 0.18; 'try:': 0.18; '&gt;': 0.18; 'input': 0.18; '(not': 0.20; 'windows': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'posted': 0.21; 'assumes': 0.22; 'fine,': 0.22; 'parse': 0.22; 'referring': 0.22; 'subject:support': 0.22; 'pass': 0.22; 'programming': 0.22; 'am,': 0.23; 'code,': 0.23; 'defined': 0.23; 'elements': 0.23; 'errors': 0.23; 'this:': 0.23; 'import': 0.24; '(most': 0.24; 'xml': 0.24; 'written': 0.24; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'script': 0.25; 'rest': 0.26; 'skip:" 20': 0.26; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'finally,': 0.27; 'skip:e 30': 0.27; 'yield': 0.27; 'function': 0.28; 'idea': 0.28; "skip:' 10": 0.28; 'values': 0.28; 'actual': 0.28; 'indentation': 0.29; 'separated': 0.29; 'skip:/ 80': 0.29; 'skip:2 30': 0.29; 'unlikely': 0.29; 'environment': 0.29; 'raise': 0.29; 'url:mailman': 0.30; 'code': 0.30; 'skip:& 30': 0.30; 'error.': 0.31; 'another': 0.32; 'included': 0.32; 'language.': 0.32; 'run': 0.33; 'useful': 0.33; 'url:python': 0.33; 'message.': 0.33; 'curious': 0.33; "d'aprano": 0.33; 'extract': 0.33; 'hopefully': 0.33; 'skip:j 20': 0.33; 'steven': 0.33; 'traceback': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'except': 0.34; 'previous': 0.34; 'skip:d 20': 0.34; 'running': 0.34; 'add': 0.34; 'list': 0.34; 'said:': 0.66; 'here': 0.66; 'stand': 0.67; '8bit%:21': 0.70; 'receive': 0.71; 'treat': 0.72; '8bit%:24': 0.84; 'common,': 0.84; 'post,': 0.84; 'different.': 0.91; 'fyi': 0.91; 'same,': 0.91; '8bit%:18': 0.93; 'notebook': 0.96
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=XDF4nbUNr/NZmj2DxXGl+M6Jk+YTiQfpsmZsVzQbQzE=; b=meMnLvFEf6iyduwwdKBnjsOvHtSzpp/hlE9kznqv3LEsdCSP0bOh4/bFhJdXLeKR+3 kILRQMsaIH+IneIjox9HHle5roWS10Dz2cS+ZTDCUsW0Prx+3oB6yVEeWONf8Hdu9YPX JRVl4FNUB0cuGuO9FC+E78X15eHbX6kDPAeeO/3Yr8YOFk42P/IeAqmMjAkQIOZeGFEQ qZcCVYBJEOPS0Bfpp01nFBfroqqMEDGQ5FyDWyEWDMSQFBRWG4LeYlI4z7Qx460kZZML JKcKxZhJmYleZ/3yFzjb6h9sfgkEfOypXZehHTPVdmA8u53SoLR9ym7TwJVjFMhFpaJb pn8w==
X-Received by 10.28.16.78 with SMTP id 75mr9663541wmq.82.1452448427645; Sun, 10 Jan 2016 09:53:47 -0800 (PST)
In-Reply-To <56929498$0$1622$c3e8da3$5496439d@news.astraweb.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 <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>
Xref csiph.com comp.lang.python:101441

Show key headers only | View raw


Hi Steven:

The previous code was a stand along under the " if __name__ == '__main__':
". The full function suite that I have made (and indeed includes a try and
except block):

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


def flatten_list(self, aList, prefix=''):

    for i, element in enumerate(aList, 1):
        eprefix = "{}{}".format(prefix, i)
        if element:
            # treat like dict
            if len(element) == 1 or element[0].tag != element[1].tag:
                yield from flatten_dict(element, eprefix)
            # treat like list
            elif element[0].tag == element[1].tag:
                yield from flatten_list(element, eprefix)
        elif element.text:
            text = element.text.strip()
            if text:
                yield eprefix[:].rstrip('.'), element.text


def flatten_dict(parent_element, prefix=''):

    prefix = prefix + parent_element.tag
    if parent_element.items():
        for k, v in parent_element.items():
            yield prefix + k, v
    for element in parent_element:
        eprefix = element.tag
        if element:
            # treat like dict - we assume that if the first two tags
            # in a series are different, then they are all different.
            if len(element) == 1 or element[0].tag != element[1].tag:
                yield from flatten_dict(element, prefix=prefix)
            # treat like list - we assume that if the first two tags
            # in a series are the same, then the rest are the same.
            else:
                # here, we put the list in dictionary; the key is the
                # tag name the list elements all share in common, and
                # the value is the list itself
                yield from flatten_list(element, prefix=eprefix)
            # if the tag has attributes, add those to the dict
            if element.items():
                for k, v in element.items():
                    yield eprefix+k
        # this assumes that if you've got an attribute in a tag,
        # you won't be having any text. This may or may not be a
        # good idea -- time will tell. It works for the way we are
        # currently doing XML configuration files...
        elif element.items():
            for k, v in element.items():
                yield eprefix+k
        # finally, if there are no child tags and no attributes, extract
        # the text
        else:
            yield eprefix, element.text



def just_xml_data(path):
    with open(path, 'rU', encoding='UTF-8') as data:
        separated = data.read().split('","')
        print(separated)
        try:
        x = ElementTree.XML(separated[3])
        print(x)
        xml.etree.ElementTree.dump(x)
        y = ElementTree.XML(separated[4])
        xml.etree.ElementTree.dump(y)
            # response = ElementTree.XML(separated[4])  # work on the
Response column
            # root = ElementTree.XML(response) #serialize and parse into
XML object
        except Exception as e:
            print(e)
        else:
            xml_field = dict(flatten_dict(y))
            return xml_field

def read_data(path):
    headers= set()
    rows = []
    with open(path, 'rU', encoding='utf-8') as data:
        reader = csv.DictReader(data, dialect=csv.excel,
skipinitialspace=True)
        for row in reader:
            xml_field = row["CLIENT_RESP_DATA"]
            # xml_data = just_xml_data(xml_field) ## function
            if xml_data is not None:
                row.update(xml_data)
                headers.update(row.keys())
                rows.append(row)
            else:
                print("Failure")
                pass
    with open(os.path.splitext(textFile)[0] + '_' + 'parsed' + '.csv',
"wt", newline='') as output_file:
        wr = csv.writer(output_file)
        csv_headers = list(headers)
        wr.writerow(csv_headers)
        for row in rows:
            values = []
            for field in csv_headers:
                value = row.get(field, None)
                values.append(value)
            wr.writerow(values)
    return output_file



if __name__ == '__main__':
Response = "s.csv"
    just_xml_data(Response)


Hopefully this will provide you with enough information to emulate
(apologies for any and all indentation errors during the copy and paste).
FYI - I still receive the same error.


On Sun, Jan 10, 2016 at 12:27 PM, Steven D'Aprano <steve@pearwood.info>
wrote:

> On Mon, 11 Jan 2016 02:04 am, kbtyo wrote:
>
> > Hello Everyone:
> >
> > I am curious to know why I receive the aforementioned message. I am using
> > Python 3.4.3 and Windows 7. I am running the following script from
> Windows
> > Powershell:
>
> I created a file "data" containing the input data you said:
>
> > The input data is as follows:
> >
> > A,B,C,D,E,F,G,H,I,J
> > "3","8","1","<Request TransactionID="3" RequestType="FOO"><InstitutionISO
> > /><CallID>23</CallID><MemberID>12</MemberID><MemberPassword
> >
> /><RequestData><AccountNumber>2</AccountNumber><AccountSuffix>85</AccountSuffix><AccountType>S</AccountType><MPIAcctType>Checking</MPIAcctType><TransactionCount>10</TransactionCount></RequestData></Request>","<Response
> > TransactionID="2"
> >
>
> RequestType="HoldInquiry"><PulledLoans>True</PulledLoans><PulledClosedLoans>False</PulledClosedLoans><PulledInvestments>False</PulledInvestments><PulledClosedInvestments>False</PulledClosedInvestments><PulledCards>False</PulledCards><ShareList>0000',0001,0070,</ShareList></Response>","1967-12-25
> > 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"
>
>
>
> and then a script containing the code you said you used:
>
> > import xml.etree.cElementTree as ElementTree
> > from xml.etree.ElementTree import XMLParser
>
> > Response = 's.csv'
> > with open(Response, 'rU', encoding='utf-8') as data:
> >     separated = data.read().split('","')
> >     x = ElementTree.XML(separated[3])
> >     y = ElementTree.XML(separated[4])
> >     print(dict(flatten_dict(x)))
> >     print(dict(flatten_dict(y)))
>
>
> I get a completely different error to you, complete with traceback as
> expected:
>
> Traceback (most recent call last):
>   File "/tmp/testxml.py", line 9, in <module>
>     print(dict(flatten_dict(x)))
> NameError: name 'flatten_dict' is not defined
>
>
> This shows me three things:
>
> (1) The calls to ElementTree.XML work fine, and don't raise an exception;
>
> (2) There is no error message referring to xml.etree.ElementTree.Element or
> the buffer interface;
>
> (3) The code you posted is clearly not the code you actually ran. At the
> very least, it is not *all* the code you ran.
>
> We cannot tell what it wrong with your code if you don't show us the code
> that fails. I suggest you read this webpage:
>
> http://www.sscce.org/
>
> and follow the advice given. It's written for Java, but applies to any
> programming language. Hopefully you will either solve your problem, or be
> able to generate a sufficiently small piece of code that we can work with.
>
>
> You also suggest that your code works when running in a Jupyter Notebook.
> It
> is unlikely (but not impossible!) that exactly the same code will run
> differently when run as a script and when run under Jupyter. More likely,
> there is some difference between the code, something you have written in
> the Notebook but not included in the script.
>
> If it is exactly the same code, then perhaps it is a difference in the two
> environments. Does Jupyter set up the environment differently to what you
> get when running a script?
>
> Finally, in another post, you state:
>
> "That is the only message (*xml.etree.ElementTree.Element' does not support
> the buffer interface"*). There is no traceback."
>
>
> That is very unlikely with the code sample you posted. If true, that gives
> more evidence that you are running code which is different from what you
> have posted here. Perhaps your ACTUAL code (not the pretend code you showed
> us) includes a try...except block like this:
>
> try:
>     some code goes here
> except Exception as err:
>     print(err)
>     sys.exit()
>
>
> or similar. If so, TAKE IT OUT. That is destroying useful debugging
> information and making it more difficult to solve your problem.
>
>
>
>
>
> --
> Steven
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" kbtyo <ahlusar.ahluwalia@gmail.com> - 2016-01-10 07:04 -0800
  Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Steven D'Aprano <steve@pearwood.info> - 2016-01-11 02:19 +1100
    Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2016-01-10 10:46 -0500
  Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Steven D'Aprano <steve@pearwood.info> - 2016-01-11 04:27 +1100
    Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2016-01-10 12:53 -0500
    Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Saran Ahluwalia <ahlusar.ahluwalia@gmail.com> - 2016-01-11 16:54 -0500
      Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Steven D'Aprano <steve@pearwood.info> - 2016-01-12 12:18 +1100

csiph-web