Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Saran Ahluwalia Newsgroups: comp.lang.python Subject: Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface" Date: Mon, 11 Jan 2016 16:54:48 -0500 Lines: 283 Message-ID: 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 P0L6hPbnEF5S0hT7Yovtng5rjk1RA9cV2OfUtcxQPeCA== 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; '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; "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; 'q&a': 0.16; 'q&a': 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; 'bytes': 0.18; 'debugging': 0.18; 'element': 0.18; 'script.': 0.18; 'try:': 0.18; '>': 0.18; 'email addr:gmail.com>': 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; 'cheers': 0.24; 'xml': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'script': 0.25; 'command': 0.26; '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; 'realize': 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:/ 20': 0.33; 'skip:j 20': 0.33; 'steven': 0.33; 'traceback': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'stand': 0.67; '8bit%:21': 0.70; 'receive': 0.71; 'treat': 0.72; '8bit%:24': 0.84; 'common,': 0.84; 'execution.': 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=kxuok3MIQUwd9sMhnr3j5YHc2pvFBEAcHtW9oEUH4IM=; b=iYJMjEMeZNAqdmVVc6rn7xZNyeoIBJrQ5piDtUkIpMLZkExX5sN6T5jh6aP54TyOQn QCoNWDJbXDkM99uzZ51Hp/jQ9nTvwLhf8p9//QUnIU1kTOV6EOc8oOlr2ZChUFis2SOC anScxhkP+tgepqSDPtM/txCtpunHDe/gbhwLfI9xnP8h/j2MPBj3argHSzgYpKvVKe2c rLYuE+k+6jQ2qp5ED2foDcT3Agouw2xdHFmbAyylTRmB19snI3vrfB+7XFoDajDf40CF 1Krub0Htsy0incVZ8Qq4XRlSb9ii+THDtht0n7k1Zm5sMH6qK5n6qPcXM1WlACw+1E2y FVIg== X-Received: by 10.28.23.5 with SMTP id 5mr16472887wmx.82.1452549307719; Mon, 11 Jan 2016 13:55:07 -0800 (PST) In-Reply-To: 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:101494 Hi Steven: Just as an update - apparently there were bytes in the Windows Command Terminal that were interrupting the process execution. I didn't realize this was happening until I dug around Windows' Q&A forum. Cheers On Sun, Jan 10, 2016 at 12:53 PM, Saran Ahluwalia < ahlusar.ahluwalia@gmail.com> wrote: > Hi Steven: > > The previous code was a stand along under the " if __name__ =3D=3D > '__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=3D''): > > for i, element in enumerate(aList, 1): > eprefix =3D "{}{}".format(prefix, i) > if element: > # treat like dict > if len(element) =3D=3D 1 or element[0].tag !=3D element[1].ta= g: > yield from flatten_dict(element, eprefix) > # treat like list > elif element[0].tag =3D=3D element[1].tag: > yield from flatten_list(element, eprefix) > elif element.text: > text =3D element.text.strip() > if text: > yield eprefix[:].rstrip('.'), element.text > > > def flatten_dict(parent_element, prefix=3D''): > > prefix =3D 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 =3D 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) =3D=3D 1 or element[0].tag !=3D element[1].ta= g: > yield from flatten_dict(element, prefix=3Dprefix) > # 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=3Deprefix) > # 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=3D'UTF-8') as data: > separated =3D data.read().split('","') > print(separated) > try: > x =3D ElementTree.XML(separated[3]) > print(x) > xml.etree.ElementTree.dump(x) > y =3D ElementTree.XML(separated[4]) > xml.etree.ElementTree.dump(y) > # response =3D ElementTree.XML(separated[4]) # work on the > Response column > # root =3D ElementTree.XML(response) #serialize and parse int= o > XML object > except Exception as e: > print(e) > else: > xml_field =3D dict(flatten_dict(y)) > return xml_field > > def read_data(path): > headers=3D set() > rows =3D [] > with open(path, 'rU', encoding=3D'utf-8') as data: > reader =3D csv.DictReader(data, dialect=3Dcsv.excel, > skipinitialspace=3DTrue) > for row in reader: > xml_field =3D row["CLIENT_RESP_DATA"] > # xml_data =3D 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=3D'') as output_file: > wr =3D csv.writer(output_file) > csv_headers =3D list(headers) > wr.writerow(csv_headers) > for row in rows: > values =3D [] > for field in csv_headers: > value =3D row.get(field, None) > values.append(value) > wr.writerow(values) > return output_file > > > > if __name__ =3D=3D '__main__': > Response =3D "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 > 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","> RequestType=3D"FOO">> > />2312> > >> />285SChecking10","> > TransactionID=3D"2" >> > >> >> RequestType=3D"HoldInquiry">TrueFalseFalse= FalseFalse<= /PulledCards>0000',0001,0070,","1967-12-2= 5 >> > 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 =3D 's.csv' >> > with open(Response, 'rU', encoding=3D'utf-8') as data: >> > separated =3D data.read().split('","') >> > x =3D ElementTree.XML(separated[3]) >> > y =3D 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 >> 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 cod= e >> 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 b= e >> able to generate a sufficiently small piece of code that we can work wit= h. >> >> >> 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 t= wo >> environments. Does Jupyter set up the environment differently to what yo= u >> 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 giv= es >> 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 >> > >