python eval list get SyntaxError: invalid syntax - python

Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> shlst="_t=[['00','bank','yx'],['04','airport','jc']]"
>>> eval(shlst)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
_t=[['00','bank','yx'],['04','airport','jc']]
^
SyntaxError: invalid syntax

Related

None difference for Python versions

I've read a lot about Python versions differences but never met those
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute '__eq__'
and
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
I found it already being in Python 3.2
Python 3.2.6 (default, Jan 18 2016, 19:21:14)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
What I wan't to know if it is told somewhere in docs or somewhere. Didn't find anything here. Any sources?

ImportError: libhdf5.so.101: cannot open shared object file: No such file or directory,

The problem occurred: ImportError: libhdf5.so.101: cannot open shared object file: No such file or directory,when I install pygrib on linux(centOS6) by Anaconda
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygrib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libhdf5.so.101: cannot open shared object file: No such file or directory
>>> exit()
================================================================
[root#localhost ll]# locate libhdf5.so.101
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
Please help me,thank you!

Python: os.environ KeyError after export

Why python can't get evironment variable after export?
How to fix this?
λ: export AUTOTEST=/home/anton/eltx/scripts/gitlab/
~
λ: python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['AUTOTEST']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'AUTOTEST'
>>>
>>> os.environ.get('AUTOTEST', 'none_value')
'none_value'
>>>
I found the mistake. There was an alias for running python

string.maketrans unicode handling: pypy vs CPython

Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).

Return value of 'print' function?

I'm python beginner.
I am wondering what the return value of the 'print' function is.
I tried type(print(3)) and didn't work.
I tried to find the api document but I could only find the pprint function.
print() returns None in Python3.
$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
3
<class 'NoneType'>
In Python2 print is a statement, so doesn't return anything...
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
File "<stdin>", line 1
type(print(3))
^
SyntaxError: invalid syntax
...unless you use from __future__ import print_function
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> type(print(3))
3
<type 'NoneType'>

Categories

Resources