Python installs, configures, and uses xlrd module, numpy module, matplotlib, and opencv module

Table of contents

 1. xlrd module

(1) Install the xlrd module

(2) pycharm configure xlrd

(3) Read xls format

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

2. numpy module

 (1) Numpy module installation---just use the Tsinghua University image to install it

(2) When pycharm is configured, the 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) Return to the interface and 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 importing numpy in the console, 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 the xlrd module

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

pip install xlrd

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


(2) pycharm configure xlrd

When the pycharm module imports the xlrd module, `import xlrd #import module`  
will prompt `No module named 'xlrd'`, indicating that the module does not exist in pytcharm and needs to be configured. The configuration 
path is: file----setting ---project--- python interpret
and then add + to add.


(3) Read xls format


The latest version of the xlrd module I installed does not support the xlsx format. It 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.

##The 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 and column 0 (sheet.cell_value(i, 0)) in the sheet to datetime or the corresponding string format

 cell = xlrd.xldate_as_datetime(sheet.cell_value(i, 0), 0).strftime('%H:%M:%S') #这是string格式
   cell = xlrd.xldate_as_datetime(sheet.cell_value(i, 0), 0)#这是datetime模式

Python xlrd reads and processes excel date type_xldate_as_tuple_Lin Xihe’s blog-CSDN blog https://blog.csdn.net/qq_51292462/article/details/123163968?ops_request_misc=%257B%2522request%255Fid%2522%253A% 2522168957525216800186544314%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168957525216800186 544314&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%5Ev88%5Econtrol_2,239%5Ev2%5Einsert_chatgpt&utm_term=xldate_as_datetime%20%20%E5%87%BD%E6%95%B0%E6%B2%A1%E6%9C%89%E5%95%8A&spm =1018.2226.3001.4187
 

2. numpy module

 (1) Numpy module installation---just use the Tsinghua University image to install it

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

Solution: Just use the image from Tsinghua University

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

Python installs numpy. python installs numpy module and reports an error_Lonely Sand Lengzhou’s technology blog_51CTO blog https://blog.51cto.com/u_39037/6616631
 

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

This is because the corresponding library is not installed in python. According to one (2), when configuring pycharam, the installation always fails.
Tried: Upgrade pip ---- no matter how to use
cmd -- when entering pip list, numpy is displayed. 

**Solution:** Add system interpreter

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


The path in python interpret is the project path. Let's add a system path. At 1, select add local interpret in add interpret. 



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



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

In the Python project, after using pip to install a third-party plug-in, I can obviously use pip list to find it, but I still can't find it when importing it in the project. What should I do? _pip installation is successful but import is unsuccessful_summer_my_sunshine's blog-CSDN blog https://blog.csdn.net/summer_my_sunshine/article/details/128062975

(3) When importing numpy in the console, it will prompt ModuleNotFoundError: No module named 'numpy'

There is no exception in numpy in pycharm, but when importing numpy using python's console interface, it will prompt ModuleNotFoundError: No module named 'numpy'

3. opencv module

If you directly enter pip install opencv-python, an error will be reported. Customize the source below and download it using the source of Tsinghua University.

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\new-code\fenxi\python_results\weizhi1.jpg', 0)
    cv2.imshow('iamge0', img)
    cv2.waitKey(0)
    # 关闭窗口后退出程序
    cv2.destroyAllWindows()

 

The running code is as follows:

4. Problems encountered during use

1. matplotlib---Chinese garbled problem

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

    plt.xlabel("像元个数", fontproperties='SimHei') #解决了中文显示乱码的问题
    plt.ylabel('x1')

Four ways for matplotlib to display Chinese_matplotlib Chinese_QQVQQ...'s blog-CSDN blog https://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%5Ev1%5Econtrol&utm_term=python%20%E4%BD%BF%E7%94%A8matplotlib%E6%97%B6% 20%E4%B8%AD%E6%96%87%E4%B8%8D%E6%98%BE%E7%A4%BA&spm=1018.2226.3001.4450

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

        plt.text(len(arrXPos)/3, np.max(arrXpos_np) , str_num)
        plt.plot(arrXPos)
        #savefig放在show前才行,要不然保存不了图片
        strFile =  r'F:\workspace\spectrum\new-code\fenxi\python_results\{0}.jpg'.format(sTempList[8])
        print(strFile)
        plt.savefig(strFile) #在show前才行
        plt.show()

3. pip list to view the imported module - just enter pip list in cmd

Guess you like

Origin blog.csdn.net/u012719076/article/details/131779252