部分用到的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里不能用括号来表示语句块,也不能用开始/结束标志符来表示,而是靠缩 ...
随机推荐
- 堆排序(C++实现)
#include<iostream> #include<vector> using namespace std; void swap(vector<int> &am ...
- hdu 4283 You Are the One 区间dp
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- JS中的各种类型转换规则(转)
JS中的类型转换非常恶心,大家都懂的,不过该学还是要学. 今天看犀牛书看到了转换规则,总结出来. X转字符串.数字.布尔值 X表示各种类型的值,直接上图: 值 转数字 转字符串 转布尔值 undefi ...
- 深入浅出设计模式——抽象工厂模式(Abstract Factory)
模式动机在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方法.但是有时候我们需要一个工厂可 ...
- 深入浅出设计模式——观察者模式(Observer Pattern)
模式动机 建立一种对象与对象之间的依赖关系,一个对象发生改变时将自动通知其他对象,其他对象将相应做出反应.在此,发生改变的对象称为观察目标,而被通知的对象称为观察者,一个观察目标可以对应多个观察者,而 ...
- Rails,uva 514
题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...
- Google Protocol Buffer的安装与.proto文件的定义
什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...
- 使用Eclipse创建maven项目
前提:Eclipse中安装了maven插件,或者Eclipse版本在Mars以上(自集成maven) 1.new project --maven project 2.默认点击next 3.选择构建类型 ...
- 【转】 TCP协议中的三次握手和四次挥手(图解)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...
- SQL Server 用户名密码查看
因为SQL Server是默认使用Windows身份验证的,很多时间就会慢慢忘记掉原来设置的密码,那么怎么重新设置用户名密码呢 这里以SQL Server2013为例,先以windows身份验证登陆进 ...