MBasith
MBasith

Reputation: 1499

Removing header from column

I am trying to do something similar to awk in Python to retrieve a specific column but need to remove the column header. Is there a quick simple way to remove the header from a column after using split? Below is my sample code.

du = '''Filesystem       Size  Used Avail Use% Mounted on
/dev/sda3         30G  4.0G   26G  14% /
devtmpfs         907M     0  907M   0% /dev
tmpfs            921M  152K  921M   1% /dev/shm
tmpfs            921M  8.5M  912M   1% /run
tmpfs            921M     0  921M   0% /sys/fs/cgroup
/dev/sda1        509M  291M  219M  58% /boot
Users            239G  164G   76G  69% /media/sf_Users
tmpfs            185M   12K  185M   1% /run/user/1002
tmpfs            185M     0  185M   0% /run/user/0'''

for line in du.splitlines():
    fields = line.split()
    f4 = (fields[4].strip('%'))
    print(f4)

Current Output:

Use
14
0
1
1
0
58
69
1
0

Desired Output:

14
0
1
1
0
58
69
1
0

Upvotes: 0

Views: 353

Answers (1)

Julien Spronck
Julien Spronck

Reputation: 15433

You can do this:

for line in du.splitlines()[1:]:
    fields = line.split()
    f4 = (fields[4].strip('%'))
    print(f4)

# 14
# 0
# 1
# 1
# 0
# 58
# 69
# 1
# 0

Upvotes: 1

Related Questions