diff --git a/gpuutil/gpuutil.py b/gpuutil/gpuutil.py index 21f192c..f27151a 100644 --- a/gpuutil/gpuutil.py +++ b/gpuutil/gpuutil.py @@ -16,7 +16,6 @@ def loadfile(path): def savefile(path, content): with open(path, 'w+', encoding='utf-8') as f: return f.write(content) - def loaddict(path): content = loadfile(path) content = content.strip() @@ -27,6 +26,11 @@ def loaddict(path): def savedict(path, dictionary): content = json.dumps(dictionary, indent=4, ensure_ascii=False) 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): pipe = os.popen(command) @@ -159,7 +163,7 @@ def get_basic_process_info_linux(): lines = output.split('\n')[1:] processes = {} for line in lines: - words = [p for p in line.split(' ') if p != ''] + words = clean_split(line) if len(words) < 3: continue 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. content = loadfile(filepath) lines = content.split('\n') - header = lines[0].split(' ') - header = [w.strip() for w in header] - header = [w for w in header if w != ''] + header = clean_split(lines[0]) interested = { 'user': None, 'pid': None, @@ -209,7 +211,7 @@ def get_basic_process_info_by_file(filepath, col_name_trans=None): interested[word] = i processes = {} for line in lines[1:]: - words = line.split(' ') + words = clean_split(line) pid = words[interested['pid']] user = words[interested['user']] cmd = ' '.join(words[interested['command']:])