部分用到的python代码
replace file extensions
# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done
# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done
#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done
#change .html files to php
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1php/'` ; done replace string in text
:%s/str1/str2/gc 整个文档
:1,$s/str1/str2/gc 整个文档
:.,$s/str1/str2/gc 从当前到结尾 #extract a range of lines from a text
sed -n 16224,16482p filename > newfile
sed -n -e 12345p filename > newfile
sed '5!d' filename > newfile # compare substring of a field
awk '{if ( substr($1,0,7)>6435201 && substr($1,0,7)<6521605) print $0}' # find a file
find ./ -name 'my*'
# find & cp
find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \; # add numbers in a bash script
num=$((num1 + num2))
num=$(($num1 + $num2)) # also works
num=$((num1 + 2 + 3)) # ...
num=$[num1+num2] # old, deprecated arithmetic expression syntax # Assign Output of Shell Command To Variable
var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)
var=`command-name-here`
var=`command-name-here arg1`
var=`/path/to/command`
var=`/path/to/command arg1 arg2` # fg/bg/nohup
find /etc/httpd/ -name "httpd.conf" -print >find.dt 2>&1 & awk '{if ($11=="9002" && $6==0) {revenue+=$3;count++}}END{print revenue;print count}' new_ck_table
awk '{if ($3=="+1") count++;}END{print count;}' file.txt
awk 'FNR==NR{a[$1]=1; next}; {if($1 in a) print $0} ' rank02.dat 0201-all.tmp3 > rank02.tmp3 # output third column to end of each row
cut -d ":" -f 3-
# filter empty lines
grep -e '^$' -v # comment /uncomment
ctrl+v -> x
ctrl+v -> shift+i # esc # sort by one column
sort -k 1 0316.new -o 0316.new # remove leading space
sed -e 's/^[ \t]*//' # mount NFS
mount -t nfs 10.134.12.60:/data/online/public localDir ##--------------- high frequency python command ------------------------ # regex text = "<H1>title</H1>"
re.mathc('<.*>', text) # match <H1>title</H1>
re.match('<.*?>',text) # match <H1> # sum of elements in a list
sum = reduce(lambda x,y: x+y, mylist) # compare all elements in two lists
if all(x==y for x, y in zip(X, Y))
do something # get sorted dictionary by key
sorted(dd.keys())
[(key, dd[key]) for key in sorted(dd.keys())]
print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd.keys())]) # get sorted dictionary by value
[key for key in sorted(dd, key=dd.get, reverse=True)]
[(key, dd[key]) for key in sorted(dd, key=dd.get, reverse=True)]
print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd, key=dd.get, reverse=True)])
sorted(myDict.items(), key=lambda e: e[1][2]) # value is a list # get key intersection of two dictionaries
intersect = set(dict_A.keys()) & set(dict_B.keys()) # sort a list of tuple
sorted_list = sorted(tuples, key=lambda x:x[0]) # map list onto dictionary
mydict = {x.split(':')[0]:x.split(':')[1] for x in mylist} from os.path import basename
from os.path import splitext
fname = splitext(basename(fullname))[0] # sort list and return index of list
sorted_index = sorted(range(len(list)), key=lambda k: list[k]) # intersection of two lists
b1 = [...]
b2 = [...]
intersection = set(b1).intersection(b2) # string to date / date to string
import datetime
obj_date = datetime.datetime.strptime(str_date, "%Y%m%d%H%M").date()
str_date = obj_date.strftime('%Y%m%d%H%M')
obj_date = obj_date + datetime.timedelta(days=-1)
# date to timestamp
time.mktime(time.strptime('2015-03-15 00:00:00', '%Y-%m-%d %H:%M:%S')) # read first N line of a file
with open("datafile") as myfile:
head = [next(myfile) for x in xrange(N)]
print head
# remove leading whitespace in vim
%s/^\s*//g
部分用到的python代码的更多相关文章
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- if __name__== "__main__" 的意思(作用)python代码复用
if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog http://www.dabu.info/if-__-name__ ...
- Python 代码风格
1 原则 在开始讨论Python社区所采用的具体标准或是由其他人推荐的建议之前,考虑一些总体原则非常重要. 请记住可读性标准的目标是提升可读性.这些规则存在的目的就是为了帮助人读写代码,而不是相反. ...
- 一行python代码实现树结构
树结构是一种抽象数据类型,在计算机科学领域有着非常广泛的应用.一颗树可以简单的表示为根, 左子树, 右子树. 而左子树和右子树又可以有自己的子树.这似乎是一种比较复杂的数据结构,那么真的能像我们在标题 ...
- [Dynamic Language] 用Sphinx自动生成python代码注释文档
用Sphinx自动生成python代码注释文档 pip install -U sphinx 安装好了之后,对Python代码的文档,一般使用sphinx-apidoc来自动生成:查看帮助mac-abe ...
- 上传自己的Python代码到PyPI
一.需要准备的事情 1.当然是自己的Python代码包了: 2.注册PyPI的一个账号. 二.详细介绍 1.代码包的结构: application \application __init__.py m ...
- 如何在batch脚本中嵌入python代码
老板叫我帮他测一个命令在windows下消耗的时间,因为没有装windows那个啥工具包,没有timeit那个命令,于是想自己写一个,原理很简单: REM timeit.bat echo %TIME% ...
- ROS系统python代码测试之rostest
ROS系统中提供了测试框架,可以实现python/c++代码的单元测试,python和C++通过不同的方式实现, 之后的两篇文档分别详细介绍各自的实现步骤,以及测试结果和覆盖率的获取. ROS系统中p ...
- 让计算机崩溃的python代码,求共同分析
在现在的异常机制处理的比较完善的编码系统里面,让计算机完全崩溃无法操作的代码还是不多的.今天就无意运行到这段python代码,运行完,计算机直接崩溃,任务管理器都无法调用,任何键都用不了,只能强行电源 ...
- python代码缩进
习惯了java,c++之类的宽容,初学python,被它摆了道下马威,写if else,竟然必须要我正确用缩进格式,原来在python里不能用括号来表示语句块,也不能用开始/结束标志符来表示,而是靠缩 ...
随机推荐
- 《BI那点儿事》数据流转换——模糊查找转换
BI项目中经常会有一些提取,转换,数据处理(ELT)的工作,其中最主要的是处理过赃数据.假设在项目中我们向数据库中注入了测试数据,但是通过一个外键从另外一个表中载入数据的时候没有对应的数据,那么这一行 ...
- Gitblit Go
1.Download the "Gitblit Go" package from the www.gitblit.com 2.UnZip the package 3.Open th ...
- Python生成字体
Python version 2.7 required, which was not found in the registry 参考:http://www.cnblogs.com/min0208 ...
- Hibernate疑问
官方User_guide中,3.2节 JPA Bootstrapping 第一段最后一句话, The standardized approach has some limitations in cer ...
- .net 连接数据库
"@"符号是防止将后面字符串中的"\"解析为转义字符. using System.Data; using System.Data.SqlClient; ... ...
- java读取配置文件中数据
Properties pps=new Properties(); try { pps.load(new FileInputStream("src/emai ...
- Matlab 霍夫变换 ( Hough Transform) 直线检测
PS:好久没更新,因为期末到了,拼命复习中.复习久了觉得枯燥,玩玩儿霍夫变换直线检测 霍夫变换的基本原理不难,即便是初中生也很容易理解(至少在直线检测上是这样子的). 霍夫变换直线检测的基本原理:(不 ...
- ASP.NET复合控件
① DropDownList 下拉列表 会被编译为select option ps.name 服务端常用,id 客户端常用 一般用法: 一.将数据放进去 方法一:同WinForm相同,给定数据源,然后 ...
- 2016.9.18 --- Shenyang ol
1001 Resident Evil 1002 List wants to travel 1003 hannnnah_j’s Biological Test 1004 Mathematician QS ...
- 0511Scrum项目3.0
一.product backlog ID name Imp Est how to demo notes 1 主界面 3 3 用来登陆,离开,设置,关于按钮 2 算术界面 5 10 先用背景图来装 ...