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


Groups > comp.lang.python > #95761

Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought

Subject Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought
References <cbcf05be-53d0-4b8b-b72b-1898cd4ad245@googlegroups.com>
From MRAB <python@mrabarnett.plus.com>
Date 2015-08-30 03:48 +0100
Newsgroups comp.lang.python
Message-ID <mailman.130.1440902939.11709.python-list@python.org> (permalink)

Show all headers | View raw


On 2015-08-30 03:05, kbtyo wrote:
> I am using Jupyter Notebook and Python 3.4. I have a data structure in the format, (type list):
>
> [{'AccountNumber': N,
> 'Amount': '0',
>   'Answer': '12:00:00 PM',
>    'ID': None,
>    'Type': 'WriteLetters',
>    'Amount': '10',
>    {'AccountNumber': Y,
>        'Amount': '0',
>        'Answer': ' 12:00:00 PM',
>         'ID': None,
>        'Type': 'Transfer',
>        'Amount': '2'}]
>
> The end goal is to write this out to CSV.
>
> For the above example the output would look like:
>
> AccountNumber, Amount, Answer, ID, Type, Amount
> N,0,12:00:00 PM,None,WriteLetters,10
> Y,2,12:00:00 PM,None,Transfer,2
>
> Below is the function that I am using to write out this data structure. Please excuse any indentation formatting issues. The data structure is returned through the function "construct_results(get_just_xml_data)".
>
> The data that is returned is in the format as above. "construct_headers(get_just_xml_data)" returns a list of headers. Writing out the row for "headers_list" works.
>
> The list comprehension "data" is to maintain the integrity of the column headers and the values for each new instance of the data structure (where the keys in the dictionary are the headers and values - row instances). The keys in this specific data structure are meant to check if there is a value instance, and if there is not - place an ''.
>
> def write_to_csv(results, headers):
>
>      headers = construct_headers(get_just_xml_data)
>      results = construct_results(get_just_xml_data)
>      headers_list = list(headers)
>
>      with open('real_csv_output.csv', 'wt') as f:
>          writer = csv.writer(f)
>          writer.writerow(headers_list)
>          for row in results:
>              data = [row.get(index, '') for index in results]
>          writer.writerow(data)
>
>
>
> However, when I run this, I receive this error:
>
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> <ipython-input-747-7746797fc9a5> in <module>()
> ----> 1 write_to_csv(results, headers)
>
> <ipython-input-746-c822437eeaf0> in write_to_csv(results, headers)
>        9         writer.writerow(headers_list)
>       10         for item in results:
> ---> 11             data = [item.get(index, '') for index in results]
>       12         writer.writerow(data)
>
> <ipython-input-746-c822437eeaf0> in <listcomp>(.0)
>        9         writer.writerow(headers_list)
>       10         for item in results:
> ---> 11             data = [item.get(index, '') for index in results]
>       12         writer.writerow(data)
>
> TypeError: unhashable type: 'dict'
>
>
> I have done some research, namely, the following:
>
> https://mail.python.org/pipermail//tutor/2011-November/086761.html
>
> http://stackoverflow.com/questions/27435798/unhashable-type-dict-type-error
>
> http://stackoverflow.com/questions/1957396/why-dict-objects-are-unhashable-in-python
>
> However, I am still perplexed by this error. Any feedback is welcomed. Thank you.
>
You're taking the index values from 'results' instead of 'headers'.

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


Thread

TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-29 19:05 -0700
  Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-30 03:47 +0100
    Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 09:35 -0700
      Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Laura Creighton <lac@openend.se> - 2015-08-30 19:24 +0200
        Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 10:34 -0700
          Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Laura Creighton <lac@openend.se> - 2015-08-30 20:20 +0200
            Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 14:47 -0700
  Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought MRAB <python@mrabarnett.plus.com> - 2015-08-30 03:48 +0100
    Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 09:31 -0700
      Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought MRAB <python@mrabarnett.plus.com> - 2015-08-30 18:15 +0100
        Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 10:21 -0700
  Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Ben Finney <ben+python@benfinney.id.au> - 2015-08-30 13:04 +1000
    Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 10:02 -0700

csiph-web