简体   繁体   English

从文件中获取不同的字符串并写入 a.txt

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

I'am trying to get lines from a text file (.log) into a.txt document.我正在尝试将文本文件 (.log) 中的行放入 a.txt 文档中。

I need get into my.txt file the same data.我需要进入 my.txt 文件相同的数据。 But the line itself is sometimes different.但线路本身有时会有所不同。 From what I have seen on internet, it's usualy done with a pattern that will anticipate how the line is made.从我在互联网上看到的情况来看,通常使用一种可以预测生产线的模式来完成。

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

The line i'm working with usualy looks like this.我正在使用的线路通常看起来像这样。 The data i'am trying to collect are:我试图收集的数据是:

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

And print these data, inside a.txt file.并在 a.txt 文件中打印这些数据。 Here is my current code.这是我当前的代码。 As explained above, i'am unsure about what keyword to use for my research on google.如上所述,我不确定在谷歌研究中使用什么关键字。 And how this could be called (Because the string isn't the same?)以及如何调用它(因为字符串不一样?)

I have been looking around alot, and most of the test I have done, have allowed me to do some things, but i'am not yet able to do as explained above.我一直在环顾四周,我所做的大部分测试都允许我做一些事情,但我还不能像上面解释的那样做。 So i'am in hope for guidance here:) (Sorry if i'am noobish, I understand alot how it works, I just didn't learned language in school, I mostly do small scripts, and usualy they work fine, this time it's way harder)所以我希望在这里得到指导:) (对不起,如果我是菜鸟,我很了解它是如何工作的,我只是在学校没有学过语言,我主要做小脚本,而且通常它们工作得很好,这次这更难)

def readLog(filename):

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


    eventLog = readLog('games.log')

You'll need to read the files in "raw" mode rather than as strings.您需要以“原始”模式而不是字符串形式读取文件。 When reading the file from disk, use open(filename,'rb') .从磁盘读取文件时,使用open(filename,'rb') To use your example, I ran为了使用你的例子,我跑了

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('\\')

You'll need to know which columns contain the strings you care about.您需要知道哪些列包含您关心的字符串。 For example,例如,

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

You can figure these array positions from the sample string您可以从示例字符串中计算出这些数组位置

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

If the column positions are not consistent but the key-value pairs are always adjacent, we can leverage that如果列位置不一致但键值对总是相邻的,我们可以利用它

>>> 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