Fast n dirty
If AWK is installed:
# $2 for the second column os.system("awk '{print $2}' inputdata1.txt")
Class usage
Make a class:
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:
If inputdata1.txt looks like this:
hel lo wor ld
wor ld hel lo
You will receive the following:
print getCol('inputdata1.txt')[1]
Additional notes
- You can use
pyawk for more awk features. - If you use the Quick method, use
subprocess.Popen - You can change the delimiter
getCol('inputdata1.txt', delim=", ") - Use
filter to remove empty values ββor uncomment pass
Mehdi nellen
source share