Category Archives: Python

[Solved] ValueError: row index was 65536, not allowed by .xls format

1. Cause analysis

Xlrd and xlwt are functions used to process XLS files in Python. The maximum number of rows in a single sheet is 65535. Therefore, this error occurs when the amount of read and write data exceeds: valueerror: row index was 65536, not allowed by xls format

2.Solution

1. Method 1

The xlwt library is still used for processing, but it limits the maximum number of 5W rows of data in each sheet

# Note: What needs to be written is the two-dimensional list target_data
# Write data into excel table
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet("Sheet1")
sheet2 = workbook.add_sheet("Sheet2")
sheet3 = workbook.add_sheet("Sheet3")
al = xlwt.Alignment()
al.horz = 0x02 # Set horizontal center
# Create a style object, initialize the style
style = xlwt.XFStyle()
style.alignment = al # set horizontal center

print(len(target_data)) # Look at the length of target_data data, by checking that there are actually 14W rows, so we split into 3 sheets

for i in range(0,50000):
    for j in range(0,len(target_data[i])):
     sheet1.write(i, j, target_data[i][j], style)

for i in range(50000,100000):
    for j in range(0,len(target_data[i])):
     sheet2.write(i-50000, j, target_data[i][j], style)

for i in range(100000,len(target_data)):
    for j in range(0,len(target_data[i])):
     sheet3.write(i-100000, j, target_data[i][j], style)

# Set the width of each column
sheet1.col(0).width=256*15
sheet1.col(1).width=256*15
sheet1.col(2).width=256*15
sheet1.col(3).width=256*15
sheet2.col(0).width=256*15
sheet2.col(1).width=256*15
sheet2.col(2).width=256*15
sheet2.col(3).width=256*15
sheet3.col(0).width=256*15
sheet3.col(1).width=256*15
sheet3.col(2).width=256*15
sheet3.col(3).width=256*15

workbook.save("target_data1220.xls") # 保存到target_data.xls

2. Method 2

Using openpyxl library, the maximum number of rows that can be processed reaches 1048576

# Description: need to write is a two-dimensional list target_data
# Write data to excel table
workbook = openpyxl.Workbook() 
sheet0 = workbook.create_sheet(index=0) # create sheet0
sheet0.column_dimensions['A'].width=15 # set the width of column A
sheet0.column_dimensions['B'].width=22 # Set the width of column B
sheet0.column_dimensions['C'].width=15 # Set the width of column C
sheet0.column_dimensions['D'].width=15 # Set the width of column D
# Write data in a loop, centered and aligned
for i in range(len(target_data)):
    for j in range(len(target_data[i])):
        sheet0.cell(i+1,j+1).value = target_data[i][j] # write data
        sheet0.cell(i+1,j+1).alignment = Alignment(horizontal='center', vertical='center') # center alignment
workbook.save('test.xlsx') # Save the file

[Solved] jupyter notebook Error: ModuleNotFoundError: No module named jupyter_nbextensions_configurator

Problem description

Platform: Windows 10 professional edition, anaconda3

When starting the Jupiter notebook, there is an error message, as follows:

ModuleNotFoundError: No module named  jupyter_nbextensions_configurator

Although the jupyter lab can still be used when it is opened, the error message is always a hidden danger. Therefore, after searching the data, the following solutions are found

Solution:

python -m pip install --user jupyter_contrib_nbextensions

#jupyter contrib nbextension install --user --skip-running-check

python -m pip install --user jupyter_nbextensions_configurator

#jupyter nbextensions_configurator enable --user

Only running the above two commands solves the problem of error reporting.

[Solved] Pychar error: modulenotfounderror: no module named ‘requests_ HTML ‘solution

**About pychar’s error modulenotfounderror: no module named ‘requests_HTML ‘
code from requests_HTML import htmlsession is marked with red and an error is reported

Error reason

Pycharm did not import requests_HTML Library

Solution:

    1. 1.file — settings

    1. 2.project: pycharmprojects — Python interpreter. Above a long list of libraries displayed on the right, there is a + – △ visual symbol. Click the + sign

    1. 3.enter the added Library: requests HTML in the input box, and click Install Package in the lower left corner to install it. I have already installed it here, So the button turns gray

    1. 4.returns to the previous page. You can see the installed requests HTML package on the right. Click OK

    5. The code is not marked red yes

[Solved] Windows10 Pycharm Use Virtual Environment Error: Cannot set up a python SDK

Solution:

Configure environment variables for virtual environment

The path is the path of the Python interpreter in the virtual environment
in Python, file => settings=> project interpreter=> add=> System
interpreter, select the python path of the above virtual environment, click OK, and no more errors will be reported



[Solved] Jupyter notebook use pyLDAvis Error: modulenotfounderror: no module named ‘pyLDAvis’‘

Background of the problem

Getting started with Python
try to use pyldavis for simple topic extraction;

Problems and related codes

Pyldavis has been installed, as shown in the figure (Annotated):

but an error occurs when entering the following statement:
error code segment:

import pyLDAvis
import pyLDAvis.sklearn
pyLDAvis.enable_notebook()
pyLDAvis.sklearn.prepare(lda,tf,tf_vectorizer)

Screenshot of specific error reporting:

Solution:

Some bloggers suggested that the installation was not successful. I installed it as an administrator. I operated it, but it was useless
in fact, the final solution is very simple. I found that the kernel used by my Jupiter notebook is wrong. I used a virtual environment I set up before. Just change it to the default environment (my one is named python3).

[Solved] apex Xavier install torchvision error: illegal instruction

Description: I installed torch vision 0 eight

git clone -b v0.8.1 https://github.com/pytorch/vision.git vision-0.8.1

Installation:

cd vision-0.8.1
sudo /home/nvidia/mambaforge/envs/ultra-fast-lane/bin/python3.6 setup.py install

Note: be sure to make the path and sudo

Error: illegal instruction

Solution: reduce the numpy version. I use numpy = 1.19.3. Success

[Solved] scikit-learn Error: ModuleNotFoundError: No module named ‘sklearn.utils.linear_assignment_’

Record some scikit-learn errors

The questions are as follows:

ModuleNotFoundError: No module named ‘sklearn. utils. linear_ assignment_’

Solution:
the sciki-learn version here is too high (it was 1.0.1 before, and it should be made into 0.19.X or before).
my operation is directly in the compiler In (IDE), set -> project: XXXXXX -> Python interpreter, and directly reinstall the corresponding version.

VScode Run CONDA environment Error [How to Solve]

In the CONDA environment using vs code, running the program will report the following problems

conda activate base

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

After online search, the solution is as follows. Method 2 can be used for pro test:

Method 1:

Use the anaconda navigator or Anaconda PowerShell prompt provided by Anaconda to enter the environment, or enter CONDA Bat activate “environment path”, such as CONDA batactivate”D:\Anaconda3\envs\test”

Method 2:

Step 1:

Open the administrator power shell, modify the execution policy, execute: get executionpolicy, reply restricted, indicating that the status is prohibited

Restricted – default setting, no scripts are allowed to run
allsigned – only scripts signed by digital certificates can be run
remotesigned – local scripts do not need digital signatures, but scripts downloaded from the network must have digital signatures
unrestricted – all scripts are allowed to run

Step 2:

Execute: set executionpolicy remotesigned

Step 3:

Input: CONDA init

At this time, the configuration file profile will be generated under C:\users\XXX\documents\windowspowershell ps1

After restarting the power shell or vs code, it will enter the base environment of CONDA by default

Recovery:

Delete profile ps1.

According to personal conditions, execute the set executionpolicy restricted policy and change it back to restricted.

[Solved] SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 6-7: malformed

Successfully solved syntaxerror: (Unicode error) ‘Unicode eescape’ codec can’t decode bytes in position 6-7: malformed


solve the problem

input_image_path = ‘Inputs\Nnu01.jpg’
^
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 6-7: malformed \N character escape

Solution ideas

Syntax error: (Unicode error) the ‘Unicode escape’ codec cannot decode bytes at positions 6-7: malformed\n character escape

Solution:

Escape character problem, caused by ‘\n’ in the string! You need to modify as follows, just add r in front!

input_image_path = r'Inputs\Nnu01.jpg'

OK, it’s done!