【发布时间】:2021-12-18 23:26:51
【问题描述】:
我有一个 txt 文件(几 GB 大小!),我想从中提取 md5 哈希。这对我来说很容易,但是......单词之间每行的空格数量是不规则的。例如:
NUMBER(几个空格)LOGIN(几个空格)MAIL(几个空格)MD5 HASH(几个空格)DATE (几个空格)一些无用的字符。
md5 之后的所有数据都是无用的。 我想从这个文件中提取所有 md5 哈希,并且只提取 md5。我不需要任何其他数据。如果登录、邮件和哈希之间有一定数量的空格,这将是一件容易的事,但是......我没有足够的经验。数据之间的空格数量可能会有所不同。
感谢大家的帮助。
到目前为止,我设法更改了位于 here (md5hashes.txt) 使用此代码到this format (lines.txt):
import io
import re
errors = 0
#name of file to store addresses
file_to_save = open("md5hashes.txt", 'w') # this file contains md5 hashes with other useless data
file_to_read = "lines.txt" #and there we will store only md5
print(f"Hello. Opening {file_to_read}")
with io.open(file_to_read, mode='r', encoding='utf8') as file_to_open:
data = file_to_open.readlines()
for data_in_file in data:
file_to_save.write(str(data_in_file.split()))
file_to_open.close()
file_to_save.close()
print("All files closed.")
我认为这将删除空格并简化解析文件以删除其他无用数据。但是没有。
有什么想法吗?
也许更好的办法是检查每个字符串是否正好有 32 个字符长并且只包含 a-f 字母和 0-9 位数字,然后将其提取到另一个文件中?
【问题讨论】: