Python join, reduce split space

Asked 2 years ago, Updated 2 years ago, 122 views

I'm going to download the process list from cmd to txt and reduce the gap with Python. (tasklist | findstr > a.txt to create a text file.)

A list of processes is saved in the following format:

sms.exe (blank) (blank) (blank) 452 Services (blank) (blank) 0 (blank) (blank) (blank) 1,064 K (\n) wininit.exe (blank) (blank) (blank) 692 Services (blank) (blank) 0 (blank) (blank) (blank) 5,672 K (\n)

You want to change the delimiter to one space to load these lists into the DB. (Separators separate columns.)

When '.join(processlist) using Python's join function

sms.exe(blank)452(blank)Services0(blank)1,064(blank)K(blank)winit.exe(blank)692(blank)Services....

In this format, \n will be replaced with a single space to appear as a single line.

The format I want is

smss.exe(blank)452(blank)Services0(blank)1,064(blank)K(\n) wininit.exe(blank)692(blank)Services0(blank)5,672(blank)K

I want it to be printed like this, but is it possible with Python's join and split function?

python join split

2022-09-20 22:12

1 Answers

#anwser.bat
@echo off
tasklist > tmp.txt
for /F "tokens=1,2,3,4,5,6 skip=3" %%A in (tmp.txt) DO echo %%A %%B %%C %%D %%E %%F >> result.txt
del tmp.txt

How about using the bat file above

If you do it, the txt file as below will come out

#result.txt
...
services.exe 756 Services 0 10,296 K 
lsass.exe 828 Services 0 14,840 K 
svchost.exe 948 Services 0 2,780 K 
fontdrvhost.exe 968 Services 0 1,596 K 
svchost.exe 984 Services 0 28,296 K 
...

If you want Python, do it the way below

You will be able to see the output result like above

a = open('tmp.txt', 'r')
result = ''
for i in a:
    b = " ".join(i.split())
    b += "\n"
    result += b
print(result)


2022-09-20 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.