簡體   English   中英

從文件中獲取不同的字符串並寫入 a.txt

[英]Get different strings from a file and write a .txt

我正在嘗試將文本文件 (.log) 中的行放入 a.txt 文檔中。

我需要進入 my.txt 文件相同的數據。 但線路本身有時會有所不同。 從我在互聯網上看到的情況來看,通常使用一種可以預測生產線的模式來完成。

1525:22Player 11 spawned with userinfo: \team\b\forcepowers\0-5-030310001013001131\ip\46.98.134.211:24806\rate\25000\snaps\40\cg_predictItems\1\char_color_blue\34\char_color_green\34\char_color_red\34\color1\65507\color2\14942463\color3\2949375\color4\2949375\handicap\100\jp\0\model\desann/default\name\Faybell\pbindicator\1\saber1\saber_malgus_broken\saber2\none\sex\male\ja_guid\420D990471FC7EB6B3EEA94045F739B7\teamoverlay\1

我正在使用的線路通常看起來像這樣。 我試圖收集的數據是:

\ip\0.0.0.0
\name\NickName_of_the_player
\ja_guid\420D990471FC7EB6B3EEA94045F739B7

並在 a.txt 文件中打印這些數據。 這是我當前的代碼。 如上所述,我不確定在谷歌研究中使用什么關鍵字。 以及如何調用它(因為字符串不一樣?)

我一直在環顧四周,我所做的大部分測試都允許我做一些事情,但我還不能像上面解釋的那樣做。 所以我希望在這里得到指導:) (對不起,如果我是菜鳥,我很了解它是如何工作的,我只是在學校沒有學過語言,我主要做小腳本,而且通常它們工作得很好,這次這更難)

def readLog(filename):

  with open(filename,'r') as eventLog:
    data = eventLog.read()
    dataList = data.splitlines()
  
    return dataList


    eventLog = readLog('games.log')

您需要以“原始”模式而不是字符串形式讀取文件。 從磁盤讀取文件時,使用open(filename,'rb') 為了使用你的例子,我跑了

text_input = r"1525:22Player 11 spawned with userinfo: \team\b\forcepowers\0-5-030310001013001131\ip\46.98.134.211:24806\rate\25000\snaps\40\cg_predictItems\1\char_color_blue\34\char_color_green\34\char_color_red\34\color1\65507\color2\14942463\color3\2949375\color4\2949375\handicap\100\jp\0\model\desann/default\name\Faybell\pbindicator\1\saber1\saber_malgus_broken\saber2\none\sex\male\ja_guid\420D990471FC7EB6B3EEA94045F739B7\teamoverlay\1"
text_as_array = text_input.split('\\')

您需要知道哪些列包含您關心的字符串。 例如,

with open('output.dat','w') as fil:
    fil.write(text_as_array[6])

您可以從示例字符串中計算出這些數組位置

>>> text_as_array[6]
'46.98.134.211:24806'
>>> text_as_array[34]
'Faybell'
>>> text_as_array[44]
'420D990471FC7EB6B3EEA94045F739B7'

如果列位置不一致但鍵值對總是相鄰的,我們可以利用它

>>> text_as_array.index("ip")
5
>>> text_as_array[text_as_array.index("ip")+1]
'46.98.134.211:24806'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM