快速肮脏
如果安装了AWK:# $2 for the second column
os.system("awk '{print $2}' inputdata1.txt")
使用类
上课:class getCol:
matrix = []
def __init__(self, file, delim=" "):
with open(file, 'rU') as f:
getCol.matrix = [filter(None, l.split(delim)) for l in f]
def __getitem__ (self, key):
column = []
for row in getCol.matrix:
try:
column.append(row[key])
except IndexError:
# pass
column.append("")
return column
如果inputdata1.txt看起来像:hel lo wor ld
wor ld hel lo
你会得到这个:print getCol('inputdata1.txt')[1]
#['lo', 'ld']
附加说明您可以使用^{}来获得更多awk特性
如果使用Quick'n dirty方法,请使用subprocess.Popen
您可以更改分隔符getCol('inputdata1.txt', delim=", ")
使用filter删除空值或取消注释pass