python problem summary

1. SyntaxError: (unicode error) 'utf-8' codec can't decode byte
Reason: Chinese is output
Solution : The editor is UE, the default encoding is not utf-8, press F12, select Format to utf-8

2 . print "hello" is only supported in python2, and an error is reported in python


3. NameError: name 'raw_input' is not defined
python3 input uses input(), previous versions use raw_input()

4. Editor pyCharm
http://blog .csdn.net/u013088062/article/details/50249751Shortcut
keys
1. Ctrl + Enter: create a new line below without moving the cursor;
2. Shift + Enter: create a new line below and move to the beginning of the new line;
3. Ctrl + /: Comment (uncomment) the selected line;
4. Ctrl + Alt + L: Format the code (conflict with the QQ lock hotkey, close the QQ hotkey);
5. Ctrl + Shift + +: Expand all codes Block;
6, Ctrl + Shift + -: shrink all code blocks;
7, Ctrl + Alt + I: automatically indent lines;
8, Alt + Enter: optimize code, add packages;
9, Ctrl + Shift + F: advanced search ;
10. Alt + Shift + Q: Update the code to the remote server;

5. Python reserved characters

The following list shows reserved words in Python. These reserved words cannot be used as constants or variables, or as any other identifier names.

All Python keywords contain only lowercase letters.
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield

5. Regarding python identifiers
(1) the representatives of _foo starting with a single underscore cannot be directly accessed The class attributes of the class need to be accessed through the interface provided by the class, and cannot be imported using from xxx import *
(2) __foo starting with a double underscore represents the private member of the class;
(3) __foo__ starting and ending with a double underscore represents In Python, special method-specific identifiers, such as __init__(), represent the class constructor.

6. A module can be defined inside a package. The way Python defines a package is a little weird, suppose we have a parent folder
that has a child subfolder. There is a module a.py in the child. How to let Python know This file hierarchy? Simple
Single, put a file named _init_.py in each directory. The content of this file can be empty. This hierarchy is as follows
parent
--__init_.py
--child
-- __init_.py
--a.py
b .py

from parent.child.a import add_func

7.PyCharm install third-party library method
file --> settings -->project:pythonproject --> project Interpretor
-->click the + sign in the upper right corner, then search in the search box The third-party library that needs to be installed (search requests here) and then click Install Package in the lower left corner of the interface to install it.

PIL install
pip install pillow

8. python image recognition
pip install PIL
pip install pytesseract
Visual C++ 14+ is required to install tesseract-ocr
http://landinghub.visualstudio.com/visual-cpp-build-from PIL import Image
import pytesseract
text=pytesseract.image_to_string(Image.open('denggao.jpeg'),lang='chi_sim')
print(text

9. Package as exe
pip install pyinstaller
pyinstall hello.py
http://blog.csdn.net/zt_xcyk/ article/details/73786659?locationNum=9&fps=1

10. python win32gui find win window
http://blog.csdn.net/liuyukuan/article/details/52975730

11. Use annotations to automatically prompt method variables for third-party libraries Local variable
# type: the corresponding type
im = Image.open('d:/ddz2.png', 'r') # type: Image.Image

12. *args: The length of the input data is uncertain, and the length of the input data is determined by *args The arguments passed to the function, (output dictionary)
def show(*args):
    for i in args:
        print(i)
       
show('chen','hang','wang','yadan' )
#=============================================
chen
hang
wang
yadan

13. **kargs: The length of the input data is uncertain, the system automatically expresses any length parameter as a dict (dictionary)

def show(**kargs):
    for i in kargs.items():
        print(i)

show(name ='hangge',age=25,sex='man',school='wust')
#============================== ===============
('name', 'hangge')
('school', 'wust')
('sex', 'man')
('age', 25)

14. Pyplot draws charts
https://zhuanlan.zhihu.com/p/28048062

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361243&siteId=291194637