生成一个 dataframe

1
2
3
4
5
6
7
8
9
10
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(2, 4))
print(df)

#
# 0 1 2 3
# 0 0.714730 0.566612 0.764352 0.728862
# 1 0.823414 0.662657 0.800281 0.711702
阅读全文 »

pandas显示完整的行或列,避免省略中间行或列、以及自动换行。

1
2
3
4
5
import pandas as pd

pd.set_option('display.width', 1000)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)

1. 由字符串格式生成时间数据

1
2
3
4
5
6
7
8
9
import pandas as pd
import numpy as np

# string datetime --> pandas datetime
dt_start = '2018-07-04 12:00'
pd_dt_start = pd.datetime.strptime(dt_start, '%Y-%m-%d %H:%M')

print(dt_start) # 2018-07-04 12:00
print(pd_dt_start) # 2018-07-04 12:00:00
阅读全文 »

pip 安装 pyqt5-tools

1
pip install pyqt5-tools -i https://pypi.mirrors.ustc.edu.cn/simple/

启动 pycharm, 打开 Settings -> Tools -> External Tools

  • 点击+,添加工具

  • 添加 pyqtdesigner

1
2
3
4
5
- Name:  pyqtdesigner

- Program: C:\......\designer.exe

- Working dir: $ProjectFileDir$
  • 添加 pyuic5
1
2
3
4
5
6
7
- Name:  pyuic5

- Program: C:\......\pyuic5.exe

- Arguments: $FileName$ -o $FileNameWithoutExtension$.py

- Working dir: $FileDir$

ffmpeg 使用说明

查看使用说明:ffmpeg -h

1
2
3
4
5
6
7
8
9
# usage: 
ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

# options:
-ss: set the start time offset
-f: force format
-t: record or transcode "duration" seconds of audio/video
-r set frame rate (Hz value, fraction or abbreviation)
-y: overwrite output files
阅读全文 »

ref: excel_formulas_and_functions, https://support.office.com/en-us/article/overview-of-formulas-in-excel-ecfdc708-9162-49e8-b993-c311f47ca173?wt.mc_id=fsn_excel_formulas_and_functions

0 示例表格

A B C
1 NO key value
2 1 0 0
3 2 5 25
4 3 10 100
5 4 15 225
6 5 20 400
7 6 25 625
8 7 30 900
9 8 35 1225
10 9 40 1600
11 10 45 2025

1 已知key,查找对应value

  • VLOOKUP(lookup value, lookup array, column, range lookup)
F G H
4 key Formula value
5 20 =VLOOKUP(F5,B2:C11,2) 400
6 25 =VLOOKUP(F6,B2:C11,2,FALSE) 625
7 24 =VLOOKUP(F7,B2:C11,2) 400
8 24 =VLOOKUP(F8,B2:C11,2,FALSE) #N/A
阅读全文 »

有道翻译 API 简介

参数说明:

  • i="text",引号内为要翻译的单词或句子,中文、英文都行,默认是中英互译;
  • doctype=xml,指定返回值的格式为 xml 格式,还可以是 textjson

其他可选:

  • type,默认是type=auto;可以指定语言,比如中文->日语:type=zh_cn2ja
  • version,指定 api 版本等

在 excel 里调用有道翻译 api

  • 首先用 WEBSERVICE 函数获得调用 api,返回值格式指定为 xml;

  • 然后用 FILTERXML 函数解析 xml,翻译的结果在 <translation> 标签里面

  • 完整代码如下:

1
=FILTERXML(WEBSERVICE("http://fanyi.youdao.com/translate?&i="&A1&"&doctype=xml"),"//translation")

这个 api 有调用次数的限制,且用且珍惜。

之前常用的物性计算软件是 Nist Refprop,在 Excel 中计算时调用起来非常好使。
但是计算方程组求解时 Excel 就不太好用了。。

发现了一个开源软件:CoolProp,它支持多种程序或语言的调用,接口比较好;
对 python 的支持也非常方便。

1. 安装 CoolProp

  • 直接使用 pip 安装:
1
pip install CoolProp
  • 或者安装开发版(稳定版偶尔有编译问题,导致安装失败):
1
2
# install the latest nightly release version
pip install -vvv --pre --trusted-host www.coolprop.dreamhosters.com --find-links http://www.coolprop.dreamhosters.com/binaries/Python/ -U --force-reinstall CoolProp

2. 基本使用

2.1 参数说明

ref: http://www.coolprop.org/coolprop/HighLevelAPI.html

D, DMASS kg/m^3 Mass density 密度
H, HMASS J/kg Enthalpy
P Pa Pressure 压力
Q mol/mol Mass vapor quality 气体占比
T K Temp 温度
A, SPEED_OF_SOUND m/s Speed of sound 声速
CONDUCTIVITY, L W/m/K Thermal conductivity 热导率
CVMASS, O J/kg/K Mass specific constant volume specific heat 定容比热容
C, CPMASS J/kg/K Mass specific constant pressure specific heat 定压比热容
I, SURFACE_TENSION N/m Surface tension 表面张力
M, MOLARMASS kg/mol Molar mass 摩尔质量
PCRIT Pa Pressure at the critical point 临界压力
TCRIT K Temperature at the critical point 临界温度
V, VISCOSITY Pa·s Viscosity 粘度
Z Compressibility factor 压缩系数
  • 示例1:
1
2
3
4
5
6
7
from CoolProp.HumidAirProp import HAPropsSI
from CoolProp.CoolProp import PropsSI

p = 101325
t = PropsSI('T', 'P', p, 'Q', 0, 'Water')
print('\n-------------CoolProp-------------')
print('- 在 {} Pa(abs) 时,水的饱和温度: {:.2f} K \n'.format(p, t)) # 373.12 K
  • 示例2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from CoolProp.HumidAirProp import HAPropsSI
from CoolProp.CoolProp import PropsSI
paras = {'D': 'kg/m^3',
'H': 'J/kg',
'A': 'J/kg/K',
'L': 'W/m/K',
'O': 'J/kg/K',
'C': 'J/kg/K',
'M': 'kg/mol',
'V': 'Pa.S',
'Z': '-'}

for (k, v) in paras.items():
v1 = CP.PropsSI(k, 'T', 273.15+20, 'P', 101325, 'water')
print(f' {k}: {v1:10.4f} {v}')
阅读全文 »

Nist Refprop 是一个常用到的物性计算软件,在 Excel 中调用起来也比较方便。

1. 安装 Nist Refprop V9.1

安装完之后:

  • 32位系统默认安装路径是 C:\Program Files\REFPROP
  • 64位系统默认安装路径是 C:\Program Files (x86)\REFPROP
阅读全文 »

1. 新建远程仓库

1
2
3
4
5
6
7
cd ~/xxxx
git init

git add .
git commit -m "first commit"
git remote add origin https://github.com/shenbo/xxxx.git
git push -u origin master
阅读全文 »

20170626

可遇不可求的事

此时此刻的你

1. os.popen 命令

python 内置的 os.popen 命令可以用于执行简单的命令。

1
2
3
4
5
6
7
8
import os

# 直接运行的命令,如:打开记事本
os.popen('notepad')

# 获得运行的log或结果,如:列出文件清单
r = os.popen('tree')
print(r.read())

2. subprocess 模块

复杂一点的命令可以使用 subprocess 模块,也是 python 内置的,可以对输入/输出进行操作;

2.1 subprocess.call 命令

1
2
3
4
5
6
7
8
9
10
11
import subprocess

# 可显示运行log/结果
subprocess.call('pip list', shell=True)

# 可交互
subprocess.call('pip uninstall pip', shell=True)

# 返回值为错误代码
retcode = subprocess.call('tree', shell=True)
print(retcode)

2.2 subprocess.Popen 命令

1
2
3
4
5
6
7
8
9
10
import subprocess

# 获得运行结果,一次读取所有,要等待命令运行结束
p = subprocess.Popen('ping baidu.com', shell=True, stdout=subprocess.PIPE).stdout
print(p.read().decode('gbk'))

# 获得运行结果,一次读取一行
p = subprocess.Popen('ping baidu.com', shell=True, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, b''):
print(line.strip().decode('gbk'))

subprocess.Popen 参数比较复杂,更多用法参见:
帮助文档

20170524

20170418

草在结它的种子

风在摇它的叶子

你们坐着,不说话

就十分美好

1. 设置默认国内源

  • Windows系统

命令行修改配置

1
2
3
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/
# pip config list

或手动修改/新建文件 C:\Users\XXXX\pip\pip.ini ,内容为:

1
2
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/

国内源

加利福尼亚大学python扩展库(非官方)

2. 更新所有安装包

1
2
3
4
5
6
7
8
9
10
11
from pip._internal.utils.misc import get_installed_distributions
import subprocess

def pip_upgrade_all():
for dist in get_installed_distributions():
print(dist)
ustc_source = 'https://pypi.mirrors.ustc.edu.cn/simple/'
cmd = 'pip install --upgrade {} -i {}'.format(dist.project_name, ustc_source)
subprocess.call(cmd, shell=True)

pip_upgrade_all()
阅读全文 »

20170218

硬件准备

  • 树莓派3B,5V 2A充电器 + 充电线
  • TF卡(>8g,class10) + 读卡器
  • 无线路由器
  • 笔记本/电脑
  • 显示器 + HDMI线

软件准备

阅读全文 »


青青紫金 悠悠我欣

20170128

出去兜风,一不小心上新闻了。

20161204

千人徒步越野 尽赏横山秋色

2016-12-03 微江宁

今天,1500多名户外徒步爱好者齐聚秋色迷人的革命老区横山,参加由江宁广电网络公司冠名的2016首届南京横溪•横山徒步越野大会。景致优美的赛道路线,准专业越野赛事的体验,让市民充分领略到全民健身的魅力。

最小登山者:四个月大的沈悠悠小朋友。

阅读原文

嘟嘟~

20161014