FAQ
I am currently using Jupyter and Pythopn 3.4. I am currently using the following script to parse and convert XML data structures, nested in a CSV - formatted file, convert into a dictionary and then append write it back into the same CSV. I am using the following (I added comments to explain the thought process):


https://gist.github.com/ahlusar1989/de2381c1fb77e96ae601


However, when I hit line 40 (referencing the gist), I receive the following error:


---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-4eff0a608e19> in <module>()
      23 # to ensure that the field names in the XML don't match (and override) the
      24 # field names already in the dictionary
---> 25 row.update(xml_data)
      26 # ensure that the headers have all the right fields
      27 headers.update(row.keys())


TypeError: 'NoneType' object is not iterable


I can only infer I am passing or converting an empty key or empty row. I welcome feedback.

Search Discussions

  • Chris Angelico at Sep 2, 2015 at 10:28 pm

    On Thu, Sep 3, 2015 at 8:15 AM, kbtyo wrote:
    However, when I hit line 40 (referencing the gist), I receive the following error:

    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-56-4eff0a608e19> in <module>()
    23 # to ensure that the field names in the XML don't match (and override) the
    24 # field names already in the dictionary
    ---> 25 row.update(xml_data)
    26 # ensure that the headers have all the right fields
    27 headers.update(row.keys())

    TypeError: 'NoneType' object is not iterable

    I can only infer I am passing or converting an empty key or empty row. I welcome feedback.

    NoneType is the type of the singleton object None. So what this means
    is that xml_data is None at this point.


    In your just_xml_data() function, which is what provides xml_data at
    that point, there are two code branches that matter here: one is the
    "else: return xml", and the other is the implicit "return None" at the
    end of the function. If you catch an exception, you print out a
    message and then allow the function to return None. Is that really
    your intention? It seems an odd way to do things, but if that is what
    you want, you'll need to cope with the None return in the main
    routine.


    BTW, your gist has a couple of non-comments on lines 48 and 49. If you
    can make sure that your code is functionally correct, it'll be easier
    for us to test. (Even more so if you can include a sample input file
    in the gist, though preferably not a huge one.) At the moment, I'm
    just eyeballing the code itself, but if someone can actually run the
    script and reproduce the exact error you're seeing, it makes debugging
    that much easier.


    All the best!


    ChrisA
  • John Gordon at Sep 2, 2015 at 10:30 pm

    In <6c0af13a-a07d-4c29-b1fd-dd53768e6408@googlegroups.com> kbtyo <ahlusar.ahluwalia@gmail.com> writes:


    https://gist.github.com/ahlusar1989/de2381c1fb77e96ae601

    Are you sure this is the actual code you're running? Lines 48 and 49 are
    clearly meant to be comments, but they do not begin with a hash sign.


    --
    John Gordon A is for Amy, who fell down the stairs
    gordon at panix.com B is for Basil, assaulted by bears
                                     -- Edward Gorey, "The Gashlycrumb Tinies"
  • Kbtyo at Sep 2, 2015 at 10:50 pm

    On Wednesday, September 2, 2015 at 6:29:05 PM UTC-4, Chris Angelico wrote:
    On Thu, Sep 3, 2015 at 8:15 AM, kbtyo wrote:
    However, when I hit line 40 (referencing the gist), I receive the following error:

    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-56-4eff0a608e19> in <module>()
    23 # to ensure that the field names in the XML don't match (and override) the
    24 # field names already in the dictionary
    ---> 25 row.update(xml_data)
    26 # ensure that the headers have all the right fields
    27 headers.update(row.keys())

    TypeError: 'NoneType' object is not iterable

    I can only infer I am passing or converting an empty key or empty row. I welcome feedback.
    NoneType is the type of the singleton object None. So what this means
    is that xml_data is None at this point.

    In your just_xml_data() function, which is what provides xml_data at
    that point, there are two code branches that matter here: one is the
    "else: return xml", and the other is the implicit "return None" at the
    end of the function. If you catch an exception, you print out a
    message and then allow the function to return None. Is that really
    your intention? It seems an odd way to do things, but if that is what
    you want, you'll need to cope with the None return in the main
    routine.

    BTW, your gist has a couple of non-comments on lines 48 and 49. If you
    can make sure that your code is functionally correct, it'll be easier
    for us to test. (Even more so if you can include a sample input file
    in the gist, though preferably not a huge one.) At the moment, I'm
    just eyeballing the code itself, but if someone can actually run the
    script and reproduce the exact error you're seeing, it makes debugging
    that much easier.

    All the best!

    ChrisA

    Hi Chris:


    I made changes to the gist: https://gist.github.com/ahlusar1989/de2381c1fb77e96ae601


    Ahhhhhh!!!! Thanks for catching that. No, I want to skip over the Exception and return the xml_data in the try block. I didn't realize that was the behaviour. I don't want to break the iterative loop. Any advice on how to absolve this?


    Thanks.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 2, '15 at 10:15p
activeSep 2, '15 at 10:50p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2019 Grokbase