文章目录
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")
strptime()
中的 p 指 parse(解析),一般解析都是说对字符串进行解析,所以strptime()
方法是将字符串解析为时间元组,通常用于计算时间。strftime()
中的 f 指 format(格式化),即将时间处理为适合查看的格式,所以strftime()
方法是将时间元组格式化为字符串,通常用于显示或存储时间。
>>> from datetime import datetime, timedelta
>>> data_str = datetime.now().strftime("%Y-%m-%d")
>>> print(data_str)
2024-11-22
>>> print(datetime.strptime(data_str, "%Y-%m-%d"))
2024-11-22 00:00:00
4. print(f “{ }”)的用法
主要作用就是格式化字符串,加 f 后可以在字符串里面使用花括号将括起来的变量和表达式的值输出。
包含在花括号 { }
里面的变量和表达式在程序运行时会被变量和表达式的值代替。
name = 'Xiaoqing'
age = '30'
# 方法一:
print(f'我是{name},今年{age}岁。')
print(f"我是{name},今年{age}岁。")
# 方法二:
print('我是{},今年{}岁。'.format(name, age))
# 方法三:
print('我是%s,今年%s岁。' % (name, age))
5. 用 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
6. 执行 sql 语句
6.1 执行 select 语句
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus as urlquote
# MySQL数据解析配置信息
MYSQL_HOST = '192.168.xx.xx'
MYSQL_PORT = '9030'
MYSQL_USER = 'root'
MYSQL_PASSWORD = ''
MYSQL_DB = 'dwd'
# MySQL获取项目配置信息
con_info = f'mysql+pymysql://{MYSQL_USER}:{urlquote(MYSQL_PASSWORD)}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DB}?charset=utf8'
mysql_engine = create_engine(con_info)
query_sql = """
SELECT student_id FROM student_day t group by student_id;
"""
df = pd.read_sql(query_sql, mysql_engine)
des_list = []
for row in df.index:
student_id = df.loc[row]['student_id']
print(student_id)
6.2 执行 insert 语句
from pymysql import Connection
con = None
try:
# 创建数据库连接
con = Connection(
host="localhost", # 主机名
port=3306, # 端口
user="root", # 账户
password="123456", # 密码
database="db_python", # 指定操作的数据库
autocommit=True # 设置自动提交
)
# 获取游标对象
cursor = con.cursor()
# 使用游标对象,执行sql语句
cursor.execute("insert into t_student values(null,'赵六2',25)")
# 获取主键
print("主键id=", con.insert_id())
# 确认提交
# con.commit()
except Exception as e:
print("异常:", e)
finally:
if con:
# 关闭连接
con.close()
注:如果字符串跨越多行,确保每一行末尾都有续行符 (\
)。否则会报错:unterminated string literal
。另一种优雅的解决办法:
insert_sql = """
insert into t_student values(null,'赵六2',25);
"""
cursor.execute(insert_sql)
7. 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.ttf
或 arial.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