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.ahluwa...@gmail.com> wrote: > 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 <st...@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 >> > > -- https://mail.python.org/mailman/listinfo/python-list