Найти - Пользователи
Полная версия: Как мне парсить мою переменную?
Начало » Python для новичков » Как мне парсить мою переменную?
1
marataziat
Я нашел либу:
 import os
from collections import namedtuple
disk_ntuple = namedtuple('partition',  'device mountpoint fstype')
usage_ntuple = namedtuple('usage',  'total used free percent')
def disk_partitions(all=False):
    """Return all mountd partitions as a nameduple.
    If all == False return phyisical partitions only.
    """
    phydevs = []
    f = open("/proc/filesystems", "r")
    for line in f:
        if not line.startswith("nodev"):
            phydevs.append(line.strip())
    retlist = []
    f = open('/etc/mtab', "r")
    for line in f:
        if not all and line.startswith('none'):
            continue
        fields = line.split()
        device = fields[0]
        mountpoint = fields[1]
        fstype = fields[2]
        if not all and fstype not in phydevs:
            continue
        if device == 'none':
            device = ''
        ntuple = disk_ntuple(device, mountpoint, fstype)
        retlist.append(ntuple)
    return retlist
def disk_usage(path):
    """Return disk usage associated with path."""
    st = os.statvfs(path)
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    try:
        percent = ret = (float(used) / total) * 100
    except ZeroDivisionError:
        percent = 0
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return usage_ntuple(total, used, free, round(percent, 1))
if __name__ == '__main__':
    forimport diskusage
gg = diskusage.disk_usage('/')
print gg part in disk_partitions():
        print part
        print "    %s\n" % str(disk_usage(part.mountpoint))
Мой код:
 import diskusage
gg = diskusage.disk_usage('/')
print gg
Мой код выводит это:
usage(total=21085020160, used=6419804160, free=13570555904, percent=30.4)

А я хочу чтобы при вызове переменной на пример total у меня вылизало 21085020160 а не usage(total=21085020160, used=6419804160, free=13570555904, percent=30.4)
Rodegast
Читай в самом низу про namedtuple https://pythonworld.ru/moduli/modul-collections.html
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB