Python代码常用功能实现总结

1. 字符串追加

json = {"HEX":"12 13 14"}
string = ''
list = json["HEX"].split(" ")
for i in range(0,len(list)):
    a = "\\x"+list[i]
    string+=a
print string

# 运行结果:
\x12\x13\x14

2. Python进阶技巧

# 条件表达式:
if x > 0:
    y = math.log(x)
else:
    y = float('nan')
# 可转换为
y = math.log(x) if x > 0 else float('nan')


# 列表式推导式:
a = []
for i in range(1000):
    if i % 2 == 0:
        a.append(i)
# 可转换为
[i for i in range(1000) if i % 2 == 0]

3. 获取昨天的字符串

(datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d")

4. 用 pandas 加载 zip 压缩包里的 csv 文件

(pyspark_env) [root@localhost ~]# python haha.py
代码执行时间: 32.55599808692932(pyspark_env) [root@localhost ~]# cat haha.py 
from zipfile import ZipFile
import pandas as pd
import time

zip_file = ZipFile('/opt/2023-08-01.zip')

def fibonacci(): 
    start_time = time.time()
    dfs = {text_file.filename: pd.read_csv(zip_file.open(text_file.filename),encoding='gbk') for text_file in zip_file.infolist() if text_file.filename.endswith('.csv')} 
    end_time = time.time()
    execution_time = end_time - start_time 
    print("代码执行时间:", execution_time, "秒")

fibonacci()

注:一开始使用的 dfs = {text_file.filename: pd.read_csv(zip_file.open(text_file.filename),encoding='utf-8') for text_file in zip_file.infolist() if text_file.filename.endswith('.csv')} 会报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbc in position 2: invalid start byte

5. Generic family ‘sans-serif‘ not found because none of the following families were found: SimHei

  当在 Linux 中使用 matplotlib 画图时,出现以下报错 findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei,其原因是缺少 SimHei 这种字体。

  (1)查看 matplotlib 支持的字体:

Python 3.11.4 (main, Jul  5 2023, 13:45:01) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> from matplotlib import font_manager
>>> font_list=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
>>> for i in font_list: print(i)
... 
DejaVu Sans
DejaVu Sans
DejaVu Sans
DejaVu Sans
DejaVu Sans Display
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Serif
DejaVu Serif
DejaVu Serif
DejaVu Serif
DejaVu Serif Display
STIXGeneral
STIXGeneral
STIXGeneral
STIXGeneral
STIXNonUnicode
STIXNonUnicode
STIXNonUnicode
STIXNonUnicode
STIXSizeFiveSym
STIXSizeFourSym
STIXSizeFourSym
STIXSizeOneSym
STIXSizeOneSym
STIXSizeThreeSym
STIXSizeThreeSym
STIXSizeTwoSym
STIXSizeTwoSym
cmb10
cmex10
cmmi10
cmr10
cmss10
cmsy10
cmtt10

  查看字体路径:

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/root/miniconda3/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc'

  查看 matplotlib 的字体缓存路径:

>>> matplotlib.get_cachedir()
'/root/.cache/matplotlib'

  (2)下载所需要字体: SimHei

  将下载好的文件放到 /root/miniconda3/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf/simhei.ttf 路径下;

  (3)删除 matplotlib 的字体缓存:rm -rf /root/.cache/matplotlib

  (4)修改 matplotlibrc 文件:

# 去掉前面的#号
font.family: sans-serif

# 去掉前面的#号,并在:号后面加上SimHei
font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

# 去掉前面的#号,并将True改为False
axes.unicode_minus: False

参考:
Generic family ‘sans-serif‘ not found because none of the following families were found: SimHei
Mac上的中文乱码问题

遇到的坑:非 root 用户无 root 权限还是报上面的那个错

  上面的方式是在 root 用户下处理的,但在没有 root 权限的用户下还是不好使。解决:

  在Linux系统个人环境根目录下(如:/home/xxx/,实际上一台服务器有多个人用,每个人都有一个环境,因此有多个环境,需要找到自己个人环境根目录),新建 ./local/share/fonts 文件夹(如果该文件夹不存在)。

[xiaoqiang@heheda ~]$ mkdir -p .local/share/fonts

  在 ./local/share/fonts 文件夹里放入已下载好的字体文件(如:arial.ttfarial.ttc 等)

[root@heheda ~]# cp /root/miniconda3/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf/simhei.ttf /home/xiaoqiang/.local/share/fonts/
[root@heheda ~]# ll /home/xiaoqiang/.local/share/fonts/
-rw-r--r--. 1 root root 10044356 4月  23 16:49 simhei.ttf
[root@heheda ~]# chown xiaoqiang:xiaoqiang /home/xiaoqiang/.local/share/fonts/
[root@heheda ~]# chown xiaoqiang:xiaoqiang /home/xiaoqiang/.local/share/fonts/simhei.ttf

  清空 matplotlib 缓存:

[xiaoqiang@heheda ~]$ rm -rf .cache/matplotlib/

  大功告成:

>>> for i in font_list: print(i)
... 
DejaVu Sans
DejaVu Sans
DejaVu Sans
DejaVu Sans
DejaVu Sans Display
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Sans Mono
DejaVu Serif
DejaVu Serif
DejaVu Serif
DejaVu Serif
DejaVu Serif Display
STIXGeneral
STIXGeneral
STIXGeneral
STIXGeneral
STIXNonUnicode
STIXNonUnicode
STIXNonUnicode
STIXNonUnicode
STIXSizeFiveSym
STIXSizeFourSym
STIXSizeFourSym
STIXSizeOneSym
STIXSizeOneSym
STIXSizeThreeSym
STIXSizeThreeSym
STIXSizeTwoSym
STIXSizeTwoSym
SimHei
cmb10
cmex10
cmmi10
cmr10
cmss10
cmsy10
cmtt10

参考:
Linux系统无root权限安装已下载字体——解决python matpotlib绘图字体缺失问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强签名设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值