
python
曹天骄
这个作者很懒,什么都没留下…
展开
-
python判断是否在docker中
设定很简单,docker中有一个文件.dockerenv,只要判断此文件是否存在,来判断python是否运行在docker环境中。代码如下:import osdef idDocker(): return os.path.exists('/.dockerenv')原创 2021-01-27 09:15:24 · 1581 阅读 · 0 评论 -
python根据文件路径获取上级目录路径
import os path = '/Users/caowei/数据集/Stable/EN-2300-103-RevA.txt'p_path = os.path.abspath(os.path.join(path, ".."))print(p_path) saveDir = os.path.dirname(path)print(saveDir)转载 2021-01-25 23:36:51 · 387 阅读 · 0 评论 -
python utc 时间
python中,我们使用datetime处理日期、时间相关。获取当前时间,主要有两个方法:datetime.nowdatetime.now():读取的时间是系统的本地时间,也就是说,如果系统时区默认没有设置,那么读取的就是世界标准。datetime.utcnow()datetime.utcnow():读取的时间一直都是系统的“世界标准时间”,不管系统的本地时区是否设置,读取的时间不会随这些设置变化。本地时间与utc时间的转化rom datetime import datetime, timed原创 2021-01-14 16:03:07 · 6205 阅读 · 1 评论 -
惊天大神坑 关于 python-socketio 与 socket.io-client 版本兼容问题
在使用fastapi进行开发中,使用python-socketio作为socketi库,前端项目使用vue开发,配合socket.io-client作为client端。我的python-socketio版本号为4.6.0前端socket.io-client我默认安装的最新的,此时最新的是3.0.4,但是发现,客户端不管怎么设置都没有用。最后发现,将socket.io-client进行降级,降到2.3.0就行了。...原创 2020-12-19 16:42:22 · 6360 阅读 · 8 评论 -
Python __call__()方法
Python 类中一个非常特殊的实例方法,即 call()。该方法的功能类似于在类中重载 () 运算符,使得类实例对象可以像调用普通函数那样,以“对象名()”的形式使用。举个例子:class CLanguage: # 定义__call__方法 def __call__(self,name,add): print("调用__call__()方法",name,add)clangs = CLanguage()clangs("曹天骄","caotianjiao")程序执行结果为转载 2020-12-16 20:50:14 · 534 阅读 · 0 评论 -
uvicorn 更改fastapi 运行host和port
在命令行输入uvicorn --help可以显示参数介绍,主要两个参数:--host TEXT Bind socket to this host. [default: 127.0.0.1]--port INTEGER Bind socket to this port. [default: 8000]所以运行命令可以改成:uvicorn app:app --host '0.0.0.0原创 2020-12-16 20:16:04 · 3571 阅读 · 0 评论 -
Python 异步操作文件 aiofiles
# 异步文件操作# pip install aiofiles# 基本用法import asyncioimport aiofilesasync def wirte_demo(): # 异步方式执行with操作,修改为 async with async with aiofiles.open("text.txt","w",encoding="utf-8") as fp: await fp.write("hello world ") print("数据写转载 2020-12-16 20:10:32 · 4390 阅读 · 0 评论 -
python保存文件,如果目录不存在,则创建
对于python 3.2 以上版本使用import osfilename = "/user/project/demo.txt"os.makedirs(os.path.dirname(filename), exist_ok=True)with open(filename, "w") as f: f.write("FOOBAR")原创 2020-11-24 09:52:24 · 4390 阅读 · 0 评论 -
python Shapely OSError: Could not find lib c or load any of its variants []
mac 升级过后,python项目运行报错python Shapely OSError: Could not find lib c or load any of its variants []。在github上找到解决办法没记录如下:To recap, I removed anything dealing with conda that depends on geos:conda remove geos, shapely, cartopyThen installed geos with brew:原创 2020-11-22 09:58:50 · 3448 阅读 · 6 评论 -
fastapi vue socket 从其他文件调用 socket 方法
需求:因为项目需要,边做边学python,这次需要使用socket功能,正常在main.py中写个socket,还是OK的,但是我想要在其他文件中,直接使用socket的emit方法,需要在文件结构上进行一些调整。使用到的第三方库:python-socketio官方地址:https://python-socketio.readthedocs.io/en/latest/1、写一下前端代码:<template> <div> <h1>Hello Socket原创 2020-09-19 16:09:42 · 987 阅读 · 0 评论 -
docker查看容器ip地址
docker inspect <containerid>比如docker inspect dcd61b573df1里面包含"Gateway": "172.17.0.1"可以读出ip原创 2020-08-27 22:46:19 · 523 阅读 · 0 评论 -
centos 安装 fortran
1、 yum install gcc-c++2、yum install gcc-gfortran原创 2020-08-27 09:22:14 · 3198 阅读 · 0 评论 -
python WindroseAxes 报错 has no attribute ‘Appender‘
引用代码from windrose import WindroseAxes报错:module 'matplotlib.docstring' has no attribute 'Appender'问题是我目前安装的python 3.8,windrose也要安装最新的。安装方法第一种:pip install git+https://github.com/python-windrose/windrose第一种速度会比较慢,可以使用第二种:git clone https://github.com原创 2020-08-26 19:42:30 · 560 阅读 · 0 评论 -
python 文件读取报错 ‘utf-8‘ codec can‘t decode
初学python遇到一个文件读取遇到问题'utf-8' codec can't decode byte 0xb0 in position 611我报错的写法with open(filename,'r') as f: LIST=f.readlines()实际问题是我的电脑默认读取编码为utf-8,但是文件的编码为gbk,只要指定好编码为gbk就OK。with open(filename,'r',encoding="") as f: LIST=f.readlines()...原创 2020-08-20 14:14:01 · 5364 阅读 · 0 评论