Tag Archives: python

When calling time module – time / datetime in wxPython, an error is reported. Valueerror: unknown locale: zh cn

1. Key words: valueerror: unknown locale: zh cn

A lot of instructions on the Internet are about the language environment. Set up a round down, or not.

2. Key words: wxPython valueerror: unknown locale: zh cn

We found the following link through the wall:

https://github.com/pyinstaller/pyinstaller/issues/4874

The corresponding relationship of the version number mentioned below

 
  PyInstaller==3.4 wxPython==4.0.4 good

  PyInstaller==3.6 wxPython==4.0.4 good

  PyInstaller==3.6 wxPython==4.0.7 good

  PyInstaller==3.6 wxPython==4.0.7.post2 good

  PyInstaller==3.6 wxPython==4.1.0 bad

 
3. Upgrade or reduce the version number of wxpathon. Then upgrade the version.

pip3 install upgrade wxPython

ModuleNotFoundError: No module named xxx

Background: I encapsulate a package and contain multiple sub packages. Each sub package has multiple different moodles. When I refer to moodles again, it is OK to run in pychar. However, when I put this folder in Linux, there is an error: modulenotfounderror: no module named ‘package’  。
The structure is as follows:

package:

    ├─ sub_package0

        │ ├─ __init__.py

        │ ├─ utils.py

        │ └─ www.py

    ├─ sub_package1

        │ ├─ __init__.py

        │ ├─ utils1.py

        │ └─ www1.py

    ....

    ├─ __init__.py

    ├─ abc.py

    └─ xyz.py

After online Baidu, mainly tried two methods:

(1) One is to add environment variable pythonpath in. Bashrc of Linux environment http://blog.sina.com.cn/s/blog_ 9b1b494a0102vrl3.html

(2) In each moudule, add a path through the sys module https://www.cnblogs.com/dreamyu/p/7889959.html

After trying the first method, we found that the above error was still reported, while the second method was too cumbersome because it had to be added to each module. In order to save trouble, put the package directly into anaconda3’s installation path anaconda3/lib/python3.6/site-packages, and the problem is solved. The site packages library is actually the storage location of the package and module installed by PIP install.

 
Supplementary knowledge

Module: module. A. Py file can be called a module. Using module can avoid the conflict between function name and variable name. Functions and variables with the same name can be stored in different modules, but it should also be noted that the names of built-in functions should not conflict with each other.

Package: package. Different modules are organized by directory, similar to folder. Modules are organized by package to avoid conflicts. After the package is introduced, as long as the top-level package name does not conflict with others, all modules will not conflict with others. Please note that there is a under each package directory__ init__. Py file, this file must exist, otherwise, python will regard this directory as a normal directory instead of a package__ init__. Py can be an empty file or have Python code, because can be an empty file__ init__. Py itself is a module, and its module name is its package name.

 

Python failed to use PIL writer library. TTF, etc. oserror: cannot open resource

Error information

Read the error information,
return freetype (font),
return freetype font (font, size, index, encoding, layout)_ Oserror: cannot open resource
generally, an error is reported. The reason is that there is a problem in using the resource file
failure reason:
1. First of all, check whether the path you bind is correct. Some people directly bind to the font file in the path of C: (Windows) fonts. This is OK, but you need to use it in Python/
2. If you bind to the font file in the path of C: (Windows) fonts, you can’t use it. Reason: the name of the font may be too long. At this time, we can rename it, Then re install to the path, or directly put your own bound path
3. The bound path is correct, but still an error is reported. Trust the font library to C:// Windows/fonts to see if it is installed. If it is not installed, it may not work

File "./ClothStore/captcha/captcha.py", line 123, in <listcomp>
    for size in font_sizes or (65, 70, 75)])
  File "/home/ClothStore/venv/lib/python3.7/site-packages/PIL/ImageFont.py", line 655, in truetype
    return freetype(font)
  File "/home/ClothStore/venv/lib/python3.7/site-packages/PIL/ImageFont.py", line 652, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/ClothStore/venv/lib/python3.7/site-packages/PIL/ImageFont.py", line 194, in __init__
    font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource

ImportError:libta_lib.so.0

The question is like the title

terms of settlement:

Installation through source code compilation

wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
Sudo make install
pip install numpy

Find the. So file location and import environment variables

find/-name libta_lib.so.0
export LD_LIBRARY_PATH=/usr/lib:$LD_LIBRARY_PATH

 

When writing a website, Vue + Flash prompts network error when visiting Vue page

Flash page is mounted at 127.0.0.1:8010
Vue page is mounted at 127.0.0.1:8080 (Axios. Defaults. Baseurl)=‘ http://127.0.0.1 :8010’)

After running, you can access port 8010, but when you access port 8080, you will be prompted with network error, and the security group port will be set to
after checking, it is found that port 8010 is forward to port 1527

guess that port 8010 is occupied
after a look, it is really

so you can directly modify the page port in app.py to 5000,
axios.defaults.baseURL = ‘ http://127.0.0.1 : 5000 ‘(or other non conflicting ports)

This time, you can directly access port 8080
it is found that using ‘127.0.0.1’ can still achieve public network page projection, but only using ‘0.0.0.0’ for flash

Error in machine learning training data (FIT): valuee rror:Unknown label type:‘continuous‘

I haven’t found the right solution on the Internet, including CSDN. They are all converted to int or multiplied by a number, but actually for the regression task, the label of floating-point number can’t be changed, which will greatly affect the error. My solution is to convert to int first, and then to floating point first

Specifically, for example, y. astype (int). Astype (float)

The matching result of Python XPath is null

When writing Python crawlers, in order to save time, you usually open F12 in the browser and right-click to copy XPath.

There is a hole in Google browser, which took half a day


The copied XPath is as follows in
Python:// * [@ id = “mainframe”]/div/table/tbody/TR/TD [1]// text ()


Use the browser plug-in XPath helper to test the match successfully!


In the Python code, you can’t match it, as follows

xxx.xpath('//*[@id="mainFrame"]/div/table/tbody/tr/td[1]//text()')

The matching result is an empty list.


reason:

The browser “optimizes” the XPath, so that the XPath copied directly from the browser can’t be run in Python.

Solution:

Delete the extra tbody. The code is as follows:

#There is an extra tbody, delete it
xxx.xpath('//*[@id="mainFrame"]/div/table/tbody/tr/td[1]//text()')
# The modified code is as follows and successfully matches.
xxx.xpath('//*[@id="mainFrame"]/div/table/tr/td[1]//text()')

The range of Chinese Unicode encoding and the calculation of the number of Chinese and English words in text

Unicode encoding range of Chinese characters

Unicode encoding range
u4e00 ~ u9fff

U + 4e00 ~ U + 9fa5 is the most commonly used range, that is, the block named CJK unified ideographs. The characters between U + 9fa6 ~ U + 9fff are still empty codes, which have not been defined yet, but there is no guarantee that they will not be defined in the future

def is_zh(char):
    """

    :param char:  Single character
    :return:
    """
    
    if u'\u4e00' <= char <= u'\u9fff':
        return True
    return False

Statistics of Chinese and English words

In word document, Review – > count the number of words can calculate the number of words, Chinese words, non Chinese words and so on, now use Python to achieve
example: Hello, world 4

# -*- coding: utf-8 -*-
import re

def strQ2B(ustring):
	# Full to half angle string
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 12288: # convert full-corner spaces directly
            inside_code = 32
        elif (inside_code >= 65281 and inside_code <= 65374): # Full-angle characters (except spaces) are converted according to the relationship
            inside_code -= 65248
        rstring += chr(inside_code)
    return rstring


def querySimpleProcess(ss):
	# query preprocessing, excluding characters other than Chinese and English numbers, all converted to lowercase
    s1=strQ2B(ss)
    s2=re.sub(r"(?![\u4e00-\u9fff]|[0-9a-zA-Z])."," ",s1) 
    s3=re.sub(r"\s+"," ",s2)
    return s3.strip().lower()


# Determine if it contains Chinese
def check_contain_chinese(check_str):
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False


# Determine if it contains English
def check_contain_english(check_str):
    for ch in check_str:
        if u'a' <= ch <= u'z' or u'A' <= ch <= u'Z':
            return True
    return False


# Delete letters from a string for character counting purposes
def delete_letters(ss):
    rs = re.sub(r"[a-zA-Z]+","",ss)
    return rs


# First space split, get the list, and then line processing each element in the list
###Example: Smart School Uniform Commercial=6, Disrespectful Breakup=2
### Exception: C Mile C Mile=3 ### Can't handle
# If the element does not contain Chinese, then the length of the element is recorded as: 1 + the number of digits
# If the element does not contain English, the length of the element is recorded as: the number of Chinese characters + the number of digits, you can directly use the len () method
# If the element contains both English and Chinese, the length of the element is recorded as: number of Chinese characters + number of digits + 1
def countCharacters(inputStr):
    tmpStr = querySimpleProcess(inputStr)
    str2list = tmpStr.strip().split(" ")
    if len(str2list) > 0:
        charsNum = 0  # Initialize character count
        for elem in str2list:
            chineseFlag = check_contain_chinese(elem)
            englishFlag = check_contain_english(elem)
            if englishFlag == False:  # no English
                charsNum = charsNum + len(elem)
                continue
            else:  # contain English
                elem = delete_letters(elem)
                charsNum = charsNum + 1 + len(elem)
        return charsNum
    return 0

[CHM] Python: How to Extract CHM Data

Demand scenario

Chm format document, extract and save as HTML

Method 1: use online conversion tools or software

Method 2: Script Compilation

Step 1: decompile. Bat script

Using this script, you can decompile the CHM file, decompress it and get the file. Some documents can be directly converted to TXT, and some will be converted to HTML. This has something to do with generating CHM files
1 create a new TXT document and write the command according to the example. After saving, save the file as . Bat format

hh -decompile Output path of conversion result File to be converted (relative/absolute path)
# Example
hh -decompile   D:\Desktop\  123.chm

2 right click the administrator to run the file, and output the result to the directory specified by the command

Step 2: HTML to TXT

[HTML] Python extracts HTML text to TXT

Pyyaml tutorial introduction to pyyaml library and YML writing and reading

PyYAML

Source code: https://github.com/yaml/pyyaml

install

# pip command line installation
pip install PyYAML

# Download the source code for installation
python setup.py install

Import

import yaml

Read yaml file

def read_yaml(yml_file, mode='r', encoding='utf-8'):
    """ Read and convert the contents of yaml to Python objects

    :param yml_file:
    :param mode:
    :param encoding:
    :return:
    """
    # safe_load_all() Open multiple documents
    with open(yml_file, mode=mode, encoding=encoding) as y_file:
        # .load is a non-recommended and unsafe encoding method
        # content = yaml.load(y_file.read(), yaml.FullLoader)
        # .safe_load safe encoding method
        # If you don't trust the input stream, you should use:
        return yaml.safe_load(y_file)

Write yaml file

def write_yaml(yaml_file, data, mode='w', encoding='utf-8', is_flush=True):
    """ Converting Python objects to yaml

    :param yaml_file:
    :param data:
    :param mode:
    :param encoding:
    :param is_flush:
    :return:
    """
    with open(yaml_file, mode=mode, encoding=encoding) as y_file:
        # yaml.dump(data, stream=y_file)
        # allow_unicode Solve the problem of writing messy code
        yaml.safe_dump(data, stream=y_file, allow_unicode=True)
        if is_flush:
            y_file.flush()

[zipfile] Python packages files as zip packages & decompresses them

Using Python to compress files, you can use the zipfile library, which provides a very rich API
zipfile itself is a context manager, and you can use with . The following is a simple demo.

pack

import os
import zipfile


def file2zip(zip_file_name: str, file_names: list):
    """ Compress and store files in multiple folders as zip
    
    :param zip_file_name: /root/Document/test.zip
    :param file_names: ['/root/user/doc/test.txt', ...]
    :return: 
    """
    # Read-write mode ZipFile requires mode 'r', 'w', 'x', or 'a'
    # Compression mode ZIP_STORED: stored; ZIP_DEFLATED: compressed storage
    with zipfile.ZipFile(zip_file_name, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
        for fn in file_names:
            parent_path, name = os.path.split(fn)
            
            # zipfile built-in provides for storing files compressed in .zip files, arcname is the name of the file stored in the zip file
            # The given archive name is arcname (by default it will be the same as filename, but without the drive letter and will remove the opening path separator)
            zf.write(fn, arcname=name)
            
            # Equivalent to the following two lines of code
            # Switch directories to write the file directly. Without switching directories, the entire path to the file is created in the zip file
            # os.chdir(parent_path)
            # zf.write(name)


if __name__ == "__main__":
	zip_name = '/root/Document/test.zip'
	files = ['/root/user/doc/test.txt', '/root/user/doc/test1.txt']
	file2zip(zip_name , files)

decompression

def zip2file(zip_file_name: str, extract_path: str, members=None, pwd=None):
    """ Folder specified by the zip file content extraction value

    :param zip_file_name: the file to be extracted .zip r'D:\Desktop\tst\tst.zip'
    :param extract_path: the directory where the extracted file is saved r'D:\Desktop\tst\test\test'
    :param members: specify the files to extract, default all
    :param pwd: the password to extract the file
    :return:
    """
    with zipfile.ZipFile(zip_file_name) as zf:
        zf.extractall(extract_path, members=members, pwd=pwd)

Python file compression

zipfile. Pyzipfile inherits from zipfile. Zipfile , and has a special writepy to package . Py . PyC