我正在建立一个系统,对一些.txt格式的.log文件进行排序,以便以后可以将其发送到excel。有70多个文件,在每一个文件im扫描关键字,我得到100+字符串,我想保存在一个.txt。我可以得到我想要的每个字符串,并查看每个日志从哪个.log文件中获取,但是现在我想用一个对应的数字(每个文件一个数字)重命名.log所来自的文件。我试过计算循环,数组和字符串,但我不会得到我的权利。。。。你知道吗
import glob
import os
import itertools
def LogFile(filename, tester):
message = []
data = []
with open(filename) as filesearch: # open search file
filesearch = filesearch.readlines() # read file
file = filename[39:]
for line in filesearch:
if tester in line: # extract ""
start = '-> '
end = ':\ '
number = line[line.find(start)+3: line.find(end)] #[ord('-> '):ord(' :\ ')]
data.append(number) # store all found wors in array
text = line[line.find(end)+3:]
message.append(text)
with open('Msg.txt', 'a') as handler: # create .txt file
for i in range(len(data)):
handler.write(f"{file}|{data[i]}|{message[i]}")
# open with 'w' to "reset" the file.
with open('Msg.txt', 'w') as file_handler:
pass
# ---------------------------------------------------------------------------------
for filename in glob.glob(r'C:\Users\\Desktop\Access\*.log'):
LogFile(filename, 'Sending Request: Tester')
现在怎么样
from .txt file
GTX77_ 2017-05-20_1209.log|166 9 02 F
GTX77_ 2017-08-24_1209.log|166 9 03 F
JBB 925_1720_1400.log|161 9 02 F
JBB 925_1724_1900.log|161 9 12 F
我多么想要它
new .txt file
1|166 9 02 F
1|166 9 03 F
2|161 9 02 F
2|161 9 12 F
因此,我的问题是,是否有函数可以将变量“filename”改为相应的数字(例如,文件GTX77.log中的所有.log文件改为索引1,JBB925中的所有.log文件改为索引2…)
你可以用字典来查。为每个文件名初始化它,然后用
get(filename, default_value)
恢复索引相关问题 更多 >
编程相关推荐