TypeError: unhashable type: 'list'
I am experimnenting with this (reproducer) code: pattern_eur= ['Total amount'] 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 5.00 linex line z line c Total amount 43598 line 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 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
Re: TypeError: unhashable type: 'list'
On Wed, Oct 23, 2019 at 3:54 AM joseph pareti 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 > > 5.00 > linex > line z > line c > Total amount > > 43598 > line > 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 > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: unhashable type: 'list'
joseph pareti wrote: > I am experimnenting with this (reproducer) code: > pattern_eur= ['Total amount'] Make that pattern_eur = 'Total amount' >match_C = re.search(pattern_eur, element) The first argument to re.search() should be a string, not a list of strings: >>> import re >>> re.search("foo", "bar") >>> re.search(["foo"], "bar") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.4/re.py", line 170, in search return _compile(pattern, flags).search(string) File "/usr/lib/python3.4/re.py", line 282, in _compile p, loc = _cache[type(pattern), pattern, flags] TypeError: unhashable type: 'list' > element = element + 2 This will be your next problem; you are adding 2 to a string. -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: unhashable type: 'list'
On 23/10/19 8:51 PM, joseph pareti wrote: I am experimnenting with this (reproducer) code: pattern_eur= ['Total amount'] 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 5.00 linex ... My intent is to locate the line containing "Total amount", skip the next line, then print the eur value. The program terminates as follows: ... Thanks for any insigths -- The first observation is that the two for loops are essentially identical, so why not condense? However, what is described may be calling for a solution called "a finite state machine": state 1: ignore unwanted data, until "Total amount" is found state 2: skip blank line state 3: grab the Euro value, and return to state 1 Being a simple-boy, I would avoid any reg-ex, because: myline[ :11 ] == "Total amount" is easier (and faster). Similarly, there is no need for rstrip-ping except at "state 3" (unless there are particular rules for the formatting of the total). Another thought is that the problem is being visualised as a series of lines and this may complicate things. If instead, a "buffer" or indeed the entire file, could be read at a time (which is current code, per first comment above), the string.find() method could be employed (replacing "state 1"), and then (implicit assumption about spacing here) "state 2" becomes a matter of moving a few characters 'along', before grabbing the total; rinse and repeat... Web-ref: https://en.wikipedia.org/wiki/Finite-state_machine -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: unhashable type: 'list'
On 23/10/19 8:51 PM, joseph pareti wrote: I am experimnenting with this (reproducer) code: pattern_eur= ['Total amount'] 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 5.00 linex ... My intent is to locate the line containing "Total amount", skip the next line, then print the eur value. The program terminates as follows: ... Thanks for any insigths -- The first observation is that the two for loops are essentially identical, so why not condense? However, what is described may be calling for a solution called "a finite state machine": state 1: ignore unwanted data, until "Total amount" is found state 2: skip blank line state 3: grab the Euro value, and return to state 1 Being a simple-boy, I would avoid any reg-ex, because: myline[ :11 ] == "Total amount" is easier (and faster). Similarly, there is no need for rstrip-ping except at "state 3" (unless there are particular rules for the formatting of the total). Another thought is that the problem is being visualised as a series of lines and this may complicate things. If instead, a "buffer" or indeed the entire file, could be read at a time (which is current code, per first comment above), the string.find() method could be employed (replacing "state 1"), and then (implicit assumption about spacing here) "state 2" becomes a matter of moving a few characters 'along', before grabbing the total; rinse and repeat... Web-ref: https://en.wikipedia.org/wiki/Finite-state_machine -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Unpickle patch: cannot instantiate 'WindowsPath' on your system
Hello, I hope that I am writing to the right list. Python 3.7.4 on GNU/Linux Parabola 5.3.1-gnu-1 x86_64 AMD Issue: I got a .pickle which had some WindowsPath inside. I was unable to unpickle like this: ┌ │ import pickle as pkl │ from pathlib import Path, PureWindowsPath, PurePath, PurePosixPath, WindowsPath, PosixPath │ fname = "ohw_analysis.pickle" │ # fpath = PureWindowsPath(fname) │ fpath = Path(fname) │ with open(fpath, "rb") as fd: │ # Works in Winbug$ only │ ohw_analysis = pkl.load(fd) └ I tried to create my own class to override =_new= in =Path=, but that did not work, because (I assume that) the pickle data was pointing to =pathlib=. I did not know how to solve this, so I modified the code for pathlib.py Solution: The modification allowed me to unpickle the file, and I think that it would not break =pathlib=. I have seen this issue reported elsewhere, and I thought that it could be useful to others. Extra: I really hope that this is useful. Although I am not asking for help, if there was a better solution, let me know. I don't want a Github account (Micro$oft). Thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: Black
On 22/10/19 3:18 AM, lizhollinshe...@gmail.com wrote: What do people think about black? I'm asking because one of my personal preferences is to use spaces for clarity: 1. right = mystr[ start : ] black version right=mystr[start:] 2. mtime = time.asctime( time.localtime( info.st_mtime ) ) black version mtime = time.asctime(time.localtime(info.st_mtime)) Is there a reason why PEP8 doesn't like these spaces? FYI: Python Bytes pod-cast "Episode #153: Auto format my Python please!" https://pythonbytes.fm/episodes/show/153/auto-format-my-python-please (topic nr6) NB I have not listened to it. [via PlanetPython] Apparently informed by blog article: "written on 06/02/2018 Auto formatters for Python 👨💻🤖" https://www.kevinpeters.net/auto-formatters-for-python -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Fursday Flippancy: American Py
[via PlanetPython] The "American Py" song. Lyrics which amused me, at https://www.reddit.com/r/Python/comments/dfm2zv/american_py/ 'Multi-taskers' may like to read and listen-along to: https://www.youtube.com/watch?v=uAsV5-Hv-7U For the benefit of us silver-surfers reliving our youth (or for those condemned to repeat history): https://en.wikipedia.org/wiki/American_Pie_%28song%29 -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
installation problem
Dear Python team I have installed Python 3.7.4 on windows 10. I have access to IDLE and I can run simple programs, but when I type python in command window nothing happens. I wanna install pip and afterward some libraries and it is when the problem occurs. why doesn't prompt window recognize python. What shall I do? Best regards -- https://mail.python.org/mailman/listinfo/python-list
Re: installation problem
On 2019-10-24 00:47, fateme jbr wrote: Dear Python team I have installed Python 3.7.4 on windows 10. I have access to IDLE and I can run simple programs, but when I type python in command window nothing happens. I wanna install pip and afterward some libraries and it is when the problem occurs. why doesn't prompt window recognize python. What shall I do? what do you mean by "nothing happens"? It should either start Python or show an error. Python 3.7 comes with pip; it should be installed already. The recommended way of starting Python on Windows these days is to use the Python launcher "py". You can use it run to pip: py -m pip install library_name -- https://mail.python.org/mailman/listinfo/python-list