TypeError: unhashable type: 'list'

Joel Goldstick joel.goldstick at gmail.com
Wed Oct 23 04:10:32 EDT 2019


On Wed, Oct 23, 2019 at 3:54 AM joseph pareti <joepareti54 at gmail.com> wrote:
>
> I am experimnenting with this (reproducer) code:
> pattern_eur    = ['Total amount']

the above line can't contain a list -- just a string

see help(re.search)

> mylines = []                                # Declare an empty list.
> with open ('tmp0.txt', 'rt') as myfile:     # Open tmp.txt for reading text.
>     for myline in myfile:                   # For each line in the file,
>         mylines.append(myline.rstrip('\n')) # strip newline and add to list.
> for element in mylines:                     # For each element in the list,
>    match_C = re.search(pattern_eur, element)
>    if match_C:
>      element = element + 2
>      print(element)
> --------------
> the input file being:
> $ cat tmp0.txt
> line 0
> line 1
> Total amount
>
> 50000.00
> linex
> line z
> line c
> Total amount
>
> 43598
> linekkkk
> line m
>
> Total amount
>
> 32000
> line end -1
> line end 0
> line end 1
> line end 2
>
> ------
>
> My intent is to locate the line containing "Total amount", skip the next
> line, then print the eur value. The program terminates as follows:
> Traceback (most recent call last):
>   File "search_then_advance.py", line 8, in <module>
>     match_C = re.search(pattern_eur, element)
>   File
> "c:\Users\joepareti\Miniconda3\pkgs\python-3.7.1-h8c8aaf0_6\lib\re.py",
> line 183, in search
>     return _compile(pattern, flags).search(string)
>   File
> "c:\Users\joepareti\Miniconda3\pkgs\python-3.7.1-h8c8aaf0_6\lib\re.py",
> line 276, in _compile
>     return _cache[type(pattern), pattern, flags]
> TypeError: unhashable type: 'list'
>
> Thanks for any insigths --
> --
> Regards,
> Joseph Pareti - Artificial Intelligence consultant
> Joseph Pareti's AI Consulting Services
> https://www.joepareti54-ai.com/
> cell +49 1520 1600 209
> cell +39 339 797 0644
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list