gpt4 book ai didi

python - 在 txt 文件中列出特定字符串然后将其放入 csv 文件中的正确方法是什么? [Python]

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:32 25 4
gpt4 key购买 nike

我有这个sample_vsdt.txt 我的 txt 文件中包含 SHA-1 和描述的文件:

Scanning samples_extracted\02b809d4edee752d9286677ea30e8a76114aa324->(Microsoft RTF 6008-0)
->Found Virus [Possible_SCRDL]

Scanning samples_extracted\0349e0101d8458b6d05860fbee2b4a6d7fa2038d->(Adobe Portable Document Format(PDF) 6015-0)
->Found Virus [TROJ_FRS.VSN11I18]

示例:

SHA-1: 02b809d4edee752d9286677ea30e8a76114aa324
Description:(Microsoft RTF 6008-0)

问题:

我的任务是在我的 txt 文件中列出这些 SHA-1 和说明,然后将其列出在 csv 文件中,我可以使用正则表达式、前缀和分隔符来做到这一点。然而这个例子让我很难:

Scanning samples_extracted\0191a23ee122bdb0c69008971e365ec530bf03f5
- Invoice_No_94497.doc->Found Virus [Trojan.4FEC5F36]->(MIME 6010-0)

- Found 1/3 Viruses in samples_extracted\0191a23ee122bdb0c69008971e365ec530bf03f5

它有不同的线路模式,我只想在第一行而不是第四行中获取 SHA-1,并在第二行中获取描述。

输出:

输出出错,因为描述 (MIME 6010-0) 被放入 SHA-1 列中。

0191a23ee122bdb0c69008971e365ec530bf03f5    
(MIME 6010-0)
02b809d4edee752d9286677ea30e8a76114aa324 (Microsoft RTF 6008-0)
0349e0101d8458b6d05860fbee2b4a6d7fa2038d (Adobe Portable Document Format(PDF) 6015-0)
035a7afca8b72cf1c05f6062814836ee31091559 (Adobe Portable Document Format(PDF) 6015-0)

代码

import csv
import re

INPUTFILE = 'samples_vsdt.txt'
OUTPUTFILE = 'output.csv'
PREFIX = '\\'
DELIMITER = '->'
DELIMITER2 = ']->'
PREFIX2 = ' - '
def read_text_file(inputfile):
data = []
with open(inputfile, 'r') as f:
lines = f.readlines()

for line in lines:
line = line.rstrip('\n')
if re.search(r'[a-zA-Z0-9]{40}', line) and not "Found" in line: # <----
line = line.split(PREFIX, 1)[-1]
parts = line.split(DELIMITER)
data.append(parts)

else:

if "->(" in line and "Found" in line :
matched_words=(re.search(r'\(.*?\)',line))
sha =(re.search(r'[a-zA-Z0-9]{40}',line))

if matched_words!=None:
matched_words=matched_words.group()
matched_words=matched_words.split("]->")

data.append(matched_words)
#data.append(parts)
return data

def write_csv_file(data, outputfile):
with open(outputfile, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
for row in data:
csvwriter.writerow(row)

def main():
data = read_text_file(INPUTFILE)
write_csv_file(data, OUTPUTFILE)

if __name__ == '__main__':
main()

这是我的文本文件的完整内容: sample_vsdt.txt

最佳答案

我改变了一些逻辑,也许我可以给你一些不同的想法。基本上,它检查字符串 Scanning Samples_extracted 是否与 ( 一起出现,这意味着描述sha 位于同一行

否则,只有扫描样本提取意味着desc位于以下行(在您的示例中有一些空行,我必须添加一个while循环)

打印结果,挑选逻辑并放入您的程序中。

import re

with open("vCjjGQxe.txt") as f:
for line in f:
if "Scanning samples_extracted" in line and "(" in line:
sha = re.search('\\\(.*)->', line).group(1)
desc = re.search('->\((.*)\)', line).group(1)
print("SHA-1:", sha)
print("Description:", desc)
continue
if "Scanning samples_extracted" in line:
sha = re.search('\\\(.*)$', line).group(1)
while True:
i = next(f)
if "(" in i:
desc = re.search('->\((.*)\)', i).group(1)
break
print("SHA-1:", sha)
print("Description:", desc)

关于python - 在 txt 文件中列出特定字符串然后将其放入 csv 文件中的正确方法是什么? [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52590373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com