Python installation, configuration, use xlrd module, numpy module, matplotlib, opencv module

Directory

1. xlrd module

(1) Install the xlrd module

(2) pycharm configuration xlrd

(3) Read xls format

(4) When xlrd reads the time and date, it will be of float type and needs to be converted.

Two, numpy module

(1) numpy module installation — use Tsinghua University mirror to install

(2) When pycharm is configured, numpy installation fails, and No module named ‘numpy’ appears in the code

(1) file—-setting—project—python interpret

?Edit (2) Select the python installation path in system interpret and add it.

(3) Go back to the interface, select the newly added system interpreter location, numpy will be on the list, and there will be no error message when importing numpy in the code.

(3) When the console imports numpy, it will prompt ModuleNotFoundError: No module named ‘numpy’


1. xlrd module

The xlrd module is divided into two steps: python installation and pycharm configuration

(1) Install xlrd module

Use windows + R to enter cmd to open **cmd**, enter

pip install xlrd

Press enter to complete the installation. If you want to upgrade, follow the prompts to complete

(2) pycharm configuration xlrd

When the pycharm module imports the xlrd module, `import xlrd #import module`
It will prompt `No module named ‘xlrd’`, indicating that there is no such module in pytcharm and needs to be configured
Configuration path: file—-setting —project—python interpret
Then add + to add.

(3) Read xls format

The latest version of the xlrd module I installed does not support the xlsx format, only supports xls, and needs to be saved as xls.

(4) When xlrd reads the time and date, it will be of float type and needs to be converted.

##Data format is

10:39:49 len=1800 m_bMeasureStatus=0 2035 570.97561

The following two statements convert the time format in row i, column 0 ( sheet.cell_value(i, 0) ) in the sheet table to datetime or the corresponding string format

 cell = xlrd.xldate_as_datetime(sheet.cell_value(i, 0), 0).strftime('%H:%M:%S') #This is a string format
 cell = xlrd.xldate_as_datetime(sheet.cell_value(i, 0), 0)#This is datetime mode

Python xlrd reads and processes excel date type /blog_editor_html/release2.3.5/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N6B9″>https://blog.csdn.net/qq_51292462/article/details/123163968?ops_request_misc=%7B%22request %5Fid%22%3A%22168957525216800186544314%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fall.%22%7D &request_id=1689575252168001865443 14 &biz_id=0&utm_medium=distribute.pc_search_result. none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-123163968-null-null.142^v88^control_2,239^v2^insert_chatgpt & amp;utm_term=xldate_as_datetime There is no function & amp;spm=1018.2226. 3001.4187

2. numpy module

(1) numpy module installation — use Tsinghua University image to install

Open cmd and enter pip install numpy will report an error, as follows

Solution: Just use the mirror image of Tsinghua University

pip install numpy scipy matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

python installs numpy python installs numpy module reports error /blog_editor_html/release2.3.5/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N6B9″>https://blog.51cto.com/u_39037/6616631

(2) When pycharm is configured, numpy installation fails, and No module named ‘numpy’ appears in the code

This is because there is no corresponding library installed in python. According to one (two), when configuring pycharam, the installation always fails.
Tried: upgrade pip —- doesn’t work
In cmd–when entering pip list, it shows numpy

**Solution:** Add a system interpreter

(1) file—-setting —project—python interpret

The path in python interpret is the project path, let’s add a system path, 1, select add local interpret in add interpret

(2) In the system interpret, select the python installation path and add it.

(3) Go back to the interface, select the newly added system interpreter location, numpy will be on the list, and there will be no error message when importing numpy in the code.

In the pyhon project, after using pip to install a third-party plug-in, it can be found by using the pip list, but what should I do if I still can’t find it when importing it in the project? _pip installed successfully but import failed_summer_my_sunshine’s blog – CSDN blog https://blog.csdn.net/summer_my_sunshine/article/details/128062975

(3) When the console imports numpy, it will prompt ModuleNotFoundError: No module named ‘numpy’

There is no abnormality in numpy in pycharm, but when using the console interface of python to import numpy, it will prompt ModuleNotFoundError: No module named ‘numpy’

3. opencv module

Entering pip install opencv-python directly will report an error, customize the source below, and use the source of Tsinghua University to download

Enter the following in cmd:

pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/

Use pip list to see that opencv is installed successfully

Test code:

 import cv2
    img = cv2.imread(r'F:\workspace\spectrum\
ew-code\fenxi\python_results\weizhi1.jpg', 0)
    cv2.imshow('iamge0', img)
    cv2.waitKey(0)
    # Exit the program after closing the window
    cv2.destroyAllWindows()

The running code is as follows:

4. Problems encountered during use

1. matplotlib—Chinese garbled characters

Modify the local font style, and add the fontproperties attribute where Chinese characters need to be displayed.

 plt.xlabel("Number of pixels", fontproperties='SimHei') #Solved the problem of garbled characters displayed in Chinese
    plt.ylabel('x1')

Four ways to display Chinese in matplotlib_matplotlib Chinese_QQVQQ…’s Blog-CSDN Bloghttps://blog.csdn.net/hfy1237/article/details/128218567?ops_request_misc= &request_id=122f0db0a2f144b78a0fa8687c169a65 &biz_id=&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~koosearch~default-2-128218567-null-null.268^v1^control l & utm_term=python Chinese is not displayed when using matplotlib&spm=1018.2226.3001.4450

2. Save the picture, savefig can only be used before show

 plt.text(len(arrXPos)/3, np.max(arrXpos_np), str_num)
        plt.plot(arrXPos)
        #savefig can only be placed before the show, otherwise the picture will not be saved
        strFile = r'F:\workspace\spectrum\
ew-code\fenxi\python_results\{0}.jpg'.format(sTempList[8])
        print(strFile)
        plt.savefig(strFile) #before show
        plt. show()

3.pip list to view the import module — enter pip list in cmd