-3

I used psutil and resource library and print the output to a file,

the output of the text file given below:

image of the output file

before argument parser:

   svmem(total=13653602304, available=12436942848, percent=8.9, used=916492288, free=8878428160, active=1242267648, inactive=3044110336, buffers=85274624, cached=3773407232, shared=36450304, slab=231096320)
   resource.struct_rusage(ru_utime=2.053535, ru_stime=0.378084, ru_maxrss=362036, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=49977, ru_majflt=813, ru_nswap=0, ru_inblock=215944, ru_oublock=752, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1610, ru_nivcsw=216)`

after argument parser:

svmem(total=13653602304, available=12436942848, percent=8.9, used=916492288, free=8878428160, active=1242267648, inactive=3044110336, buffers=85274624, cached=3773407232, shared=36450304, slab=231096320)
resource.struct_rusage(ru_utime=2.054735, ru_stime=0.378084, ru_maxrss=362036, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=49977, ru_majflt=813, ru_nswap=0, ru_inblock=215944, ru_oublock=752, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1610, ru_nivcsw=216)

after grabing the paths:

svmem(total=13653602304, available=12423421952, percent=9.0, used=930283520, free=8858972160, active=1260138496, inactive=3047149568, buffers=85520384, cached=3778826240, shared=36450304, slab=231239680)
resource.struct_rusage(ru_utime=2.062864, ru_stime=0.381083, ru_maxrss=362296, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=50058, ru_majflt=813, ru_nswap=0, ru_inblock=215944, ru_oublock=752, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1831, ru_nivcsw=217)

i want to get the number only and print it to a csv file like this:

total,available,percent,used,free,active,inactive,buffers,cached,shared,slab,ru_utime, ru_stime,ru_maxrss,ru_ixrss,ru_idrss,ru_isrss,ru_minflt,ru_majflt,ru_nswap,ru_inblock, ru_oublock,ru_msgsnd,ru_msgrcv,ru_nsignals,ru_nvcsw,ru_nivcsw 13653602304,12423421952,9.0,930283520,8858972160,1260138496,3047149568,85520384,3778826240, 36450304,231239680,2.062864,0.381083,362296,0,0,0,50058,813,0,215944,752,0,0,0,1831,217

i know it's using regex but my knowledge of regex is limited, how do i accomplish such a feat? thanks before

dedshit
  • 172
  • 10
  • Knowledge is not a fixed thing, and we should strive for improvement. Please, consider the following [guide](https://stackoverflow.com/help/how-to-ask) first. – rawrex Jun 01 '21 at 12:28
  • thanks for the review and the pre-caution, any suggestion how that i could improve this better? – MasterOfPuppetes Jun 01 '21 at 12:30
  • If you would to read your own post, you'll see that it does not incorporate any formatting which makes it hard to read first of all. As to suggestion, you should consider the link above. It would be a pleasure to help out, but not to make everything for you. Please, try something yourself. – rawrex Jun 01 '21 at 12:42

2 Answers2

2

This may suit your needs

ps =  """after grabing the paths: svmem(total=13653602304, available=12423421952, percent=9.0, used=930283520, free=8858972160, active=1260138496, inactive=3047149568, buffers=85520384, cached=3778826240, shared=36450304, slab=231239680) resource.struct_rusage(ru_utime=2.062864, ru_stime=0.381083, ru_maxrss=362296, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=50058, ru_majflt=813, ru_nswap=0, ru_inblock=215944, ru_oublock=752, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1831, ru_nivcsw=217)"""

import re
import csv

value = [[i for i in re.findall("=(\S+[ 0-9]?)[,)]", ps)]]
name = re.findall("(\w+)=", ps)

with open('datas.csv', 'w') as f:
    write = csv.writer(f)
    write.writerow(name)
    write.writerows(value)

output:

total,available,percent,used,free,active,inactive,buffers,cached,shared,slab,ru_utime,ru_stime,ru_maxrss,ru_ixrss,ru_idrss,ru_isrss,ru_minflt,ru_majflt,ru_nswap,ru_inblock,ru_oublock,ru_msgsnd,ru_msgrcv,ru_nsignals,ru_nvcsw,ru_nivcsw

13653602304,12423421952,9.0,930283520,8858972160,1260138496,3047149568,85520384,3778826240,36450304,231239680,2.062864,0.381083,362296,0,0,0,50058,813,0,215944,752,0,0,0,1831,217

screenshot: .

dedshit
  • 172
  • 10
  • Thanks for your answer, after following your lead and do some research i am finally be able to extract the data and convert it to csv. – MasterOfPuppetes Jun 02 '21 at 18:54
-1

here's the final code that i wrote, maybe some of you want it as a reference:

Here's the code:

import re
import csv

data = [] 
with open('TerminalHasilUjiCoba.txt','r') as f:
lines_ = f.read()
lines = lines_.split('\n ')

for line in lines:
  total = re.findall("total=(\S+[0-9]?)[,)]", line)[0]
  available = re.findall("available=(\S+[0-9]?)[,)]", line)[0]
  percent = re.findall("percent=(\S+[0-9]?)[,)]", line)[0]
  used = re.findall("used=(\S+[0-9]?)[,)]", line)[0]
  free = re.findall("free=(\S+[0-9]?)[,)]", line)[0]
  active = re.findall("active=(\S+[0-9]?)[,)]", line)[0]
  inactive = re.findall("inactive=(\S+[0-9]?)[,)]", line)[0]
  buffers = re.findall("buffers=(\S+[0-9]?)[,)]", line)[0]
  cached = re.findall("cached=(\S+[0-9]?)[,)]", line)[0]
  shared = re.findall("shared=(\S+[0-9]?)[,)]", line)[0]
  slab = re.findall("slab=(\S+[0-9]?)[,)]", line)[0]
  ru_stime = re.findall("ru_stime=(\S+[0-9]?)[,)]", line)[0]
  ru_maxrss = re.findall("ru_maxrss=(\S+[0-9]?)[,)]", line)[0]
  ru_ixrss = re.findall("ru_ixrss=(\S+[0-9]?)[,)]", line)[0]
  ru_idrss = re.findall("ru_idrss=(\S+[0-9]?)[,)]", line)[0]
  ru_isrss = re.findall("ru_isrss=(\S+[0-9]?)[,)]", line)[0]
  ru_minflt = re.findall("ru_minflt=(\S+[0-9]?)[,)]", line)[0]
  ru_majflt = re.findall("ru_majflt=(\S+[0-9]?)[,)]", line)[0]
  ru_nswap = re.findall("ru_nswap=(\S+[0-9]?)[,)]", line)[0]
  ru_inblock = re.findall("ru_inblock=(\S+[0-9]?)[,)]", line)[0]
  ru_oublock = re.findall("ru_oublock=(\S+[0-9]?)[,)]", line)[0]
  ru_msgsnd = re.findall("ru_msgsnd=(\S+[0-9]?)[,)]", line)[0]
  ru_msgrcv = re.findall("ru_msgrcv=(\S+[0-9]?)[,)]", line)[0]
  ru_nsignals = re.findall("ru_nsignals=(\S+[0-9]?)[,)]", line)[0]
  ru_nvcsw = re.findall("ru_nvcsw=(\S+[0-9]?)[,)]", line)[0]
  ru_nivcsw = re.findall("ru_nivcsw=(\S+[0-9]?)[,)]", line)[0]
  row = [total, available, percent, used, free, active, inactive, buffers, cached, shared, slab, ru_stime, ru_maxrss, ru_ixrss, ru_idrss, ru_isrss, ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals, ru_nvcsw, ru_nivcsw]
  data.append(row)

name = ["total", "available", "percent", "used", "free", "active", "inactive", "buffers", "cached", "shared", "slab", "ru_stime", "ru_maxrss", "ru_ixrss", "ru_idrss", "ru_isrss", "ru_minflt", "ru_majflt", "ru_nswap", "ru_inblock", "ru_oublock", "ru_msgsnd", "ru_msgrcv", "ru_nsignals", "ru_nvcsw", "ru_nivcsw"]
with open('datas2.csv', 'w') as f:
  write = csv.writer(f)
  write.writerow(name)
  for row in data:
    write.writerow(row)