문제

I am fairly new to Python, but I would like to get started and learn a bit by doing some scripting for features I will use. I have some text that is retrieved from typing in "status" in Team Fortress 2's console. What I would like to achieve is, I want to convert this text below into text where there is only the STEAM_X:X:XXXXXXXX which is the Steam64 ID.

# userid name                uniqueid            connected ping loss state
#     31 "Atonement -Ai-"    STEAM_0:1:27464943  00:48      103    0 active
#     10 "?loop?"        STEAM_0:0:31072991  40:48       62    0 active
#     11 "爱 -Ai-"          STEAM_0:0:41992530  40:46       68    0 active
#     12 "MrKateUpton -Ai-"  STEAM_0:1:10894538  40:25       81    0 active
#     13 "Tacet -Ai-"        STEAM_0:1:52131782  39:59       83    0 active
#     14 "CottonBonbon-Ai-"  STEAM_0:1:47812003  39:39       51    0 active
#     15 "belt -Ai-"         STEAM_0:1:4941202   38:43      123    0 active
#     16 "boutros :3"        STEAM_0:0:32271324  38:21       65    0 active
#     17 "[tilt] Xikkari"    STEAM_0:1:41148798  38:14       92    0 active
#     24 "ElenaWitch"        STEAM_0:0:17495028  31:30       73    0 active
#     19 "[tilt] Batcan #boutros" STEAM_0:1:41205650 38:10   63    0 active
#     20 "[?l??]whatupmydiggas" STEAM_0:1:50559125 37:58  112    0 active
#     21 "[tilt] musicman"   STEAM_0:1:37758467  37:31       89    0 active
#     22 "Jack Frost"        STEAM_0:0:24206189  37:28       90    0 active
#     28 "[tilt-sub]deaf ears #best safet" STEAM_0:1:29612138 19:05   94    0 active
#     25 "? notez ?ai"    STEAM_0:1:29663879  31:23      113    0 active
#     27 "-Ai- Lord English" STEAM_0:1:44114633  24:08      116    0 active
#     29 "1.prototypes"      STEAM_0:0:42256202  17:41       83    0 active
#     30 "SourceTV  // name for SourceTV" BOT                        active
#     32 "PUT ME IN COACH"   STEAM_0:1:48004781  00:36      173    0 spawning

Is there any built-in functions in Python that does the following algorithm?

For all that is not (!) Steam_X:X:XXXXXXXX, delete/remove.

I have done a fair amount of Googling, but nothing really gets specific. If someone could get me started with a built-in Python function, I would be so grateful to start coding.

P.S. The output would be like this

STEAM_0:1:27464943
STEAM_0:0:31072991
STEAM_0:1:10894538
etc
etc
도움이 되었습니까?

해결책

Sounds like an easy case for a regex. Assuming they're always digits like that:

>>> import re

>>> with open('/tmp/spam.txt') as f:
...   for steam64id in re.findall(r'STEAM_\d:\d:\d+', f.read()):
...     print steam64id
... 
STEAM_0:1:27464943
STEAM_0:0:31072991
STEAM_0:0:41992530
STEAM_0:1:10894538
STEAM_0:1:52131782
STEAM_0:1:47812003
STEAM_0:1:4941202
STEAM_0:0:32271324
STEAM_0:1:41148798
STEAM_0:0:17495028
STEAM_0:1:41205650
STEAM_0:1:50559125
STEAM_0:1:37758467
STEAM_0:0:24206189
STEAM_0:1:29612138
STEAM_0:1:29663879
STEAM_0:1:44114633
STEAM_0:0:42256202
STEAM_0:1:48004781

The usual recipe for removing lines is not to delete them from the original file, but print out the lines you want to keep to a new file (and then, optionally, copy it overwriting the original file, if the processing was successful).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top