Home >Operation and Maintenance >Linux Operation and Maintenance >How to solve linux python Chinese garbled characters

How to solve linux python Chinese garbled characters

藏色散人
藏色散人Original
2021-12-14 11:28:593205browse

Linux python Chinese garbled solution: 1. Check the matplotlib font location; 2. Upload the font to the server; 3. Delete the font cache; 4. Restart the python application.

How to solve linux python Chinese garbled characters

The operating environment of this article: linux5.9.8 system, Anaconda3, Dell G3 computer.

How to solve Chinese garbled code in linux python?

Solution to Chinese garbled code in python under linux

1. Scenario description

linux server Anaconda3 was installed and Pyhton's K-means algorithm was executed. The result was garbled Chinese characters as shown below. It was solved last time, but I forgot to record the solution process. This time I configured a new server, and it appeared again. If it is not configured by default, matplotlib will draw garbled characters. This time I will record the solution process, hoping to help myself and those in need. friend.

2. Solution

There are several solutions on the Internet. I only introduce the ones that are feasible in practice.

1. Check the matplotlib font location

python
import matplotlib 
print(matplotlib.matplotlib_fname())

#记录下来,字体位置我的是:/home/ruanjianlaowang/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

exit()

2. Upload the font to the server

cd /home/ruanjianlaowang/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

通过ftp或者其他方式,将字体传上去,我们传的是:simhei.ttf

3. Delete Font cache

cd  ~/.cache/matplotlib
rm -rf *

4. Restart the python application

nohup python k-means.py &

Note: If it doesn’t take effect, try changing the configuration file again . (We haven’t changed this configuration file before, but an episode happened this time. The jmeter test we used this time was still garbled. Later we changed it and it still didn’t work. It turned out to be a problem with jmeter)

cd  /home/ruanjianlaowang/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data

vi matplotlibrc 
font.family         : sans-serif  #去掉前面的#
font.serif          : simhei, DejaVu Serif,   #新加了simhei

Interlude

This time I used jmeter to test it and found that it was still garbled after all changes. Later, utf-8 parameters were added to the jmeter request.


Recommended study: "linux video tutorial"

The above is the detailed content of How to solve linux python Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn