🐛 fix an bug when parsing from file

This commit is contained in:
mingyang 2020-12-24 11:08:57 +08:00
parent 9d2faec7a5
commit 9cdd860903

View File

@ -16,7 +16,6 @@ def loadfile(path):
def savefile(path, content): def savefile(path, content):
with open(path, 'w+', encoding='utf-8') as f: with open(path, 'w+', encoding='utf-8') as f:
return f.write(content) return f.write(content)
def loaddict(path): def loaddict(path):
content = loadfile(path) content = loadfile(path)
content = content.strip() content = content.strip()
@ -27,6 +26,11 @@ def loaddict(path):
def savedict(path, dictionary): def savedict(path, dictionary):
content = json.dumps(dictionary, indent=4, ensure_ascii=False) content = json.dumps(dictionary, indent=4, ensure_ascii=False)
savefile(path, content) savefile(path, content)
def clean_split(line, delemeter=' '):
words = line.split(delemeter)
words = [w.strip() for w in words]
words = [w for w in words if w != '']
return words
def exe_cmd(command): def exe_cmd(command):
pipe = os.popen(command) pipe = os.popen(command)
@ -159,7 +163,7 @@ def get_basic_process_info_linux():
lines = output.split('\n')[1:] lines = output.split('\n')[1:]
processes = {} processes = {}
for line in lines: for line in lines:
words = [p for p in line.split(' ') if p != ''] words = clean_split(line)
if len(words) < 3: if len(words) < 3:
continue continue
username = words[0] username = words[0]
@ -191,9 +195,7 @@ def get_basic_process_info_by_file(filepath, col_name_trans=None):
# suppose cmd is always at the last, and the previous lines have no space. # suppose cmd is always at the last, and the previous lines have no space.
content = loadfile(filepath) content = loadfile(filepath)
lines = content.split('\n') lines = content.split('\n')
header = lines[0].split(' ') header = clean_split(lines[0])
header = [w.strip() for w in header]
header = [w for w in header if w != '']
interested = { interested = {
'user': None, 'user': None,
'pid': None, 'pid': None,
@ -209,7 +211,7 @@ def get_basic_process_info_by_file(filepath, col_name_trans=None):
interested[word] = i interested[word] = i
processes = {} processes = {}
for line in lines[1:]: for line in lines[1:]:
words = line.split(' ') words = clean_split(line)
pid = words[interested['pid']] pid = words[interested['pid']]
user = words[interested['user']] user = words[interested['user']]
cmd = ' '.join(words[interested['command']:]) cmd = ' '.join(words[interested['command']:])