简体   繁体   中英

python replace strings with numbers

im building a system to sort some .log files in .txt format so that I later can send it to excel. There is 70+ files and in every file im scanning for a keyword, I get 100+ strings that I want to save in a .txt. I can get every string that I want and see from which .log file each log has ben taken, but now I want to rename the file that the .log came from with a corresponding number (one number for every file). I have tried counting loops, array and strings but I wont get I right....

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

How its now

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

How I want it

new .txt file
1|166 9 02 F
1|166 9 03 F
2|161 9 02 F
2|161 9 12 F

So my question is if there is some function that can change the variable "filename" to a corresponding number instead (ex, all .log files from file GTX77.log gets index 1 instead, and all .log from JBB925 gets index 2...)

You can use a dictionary for that. you initialize it for every filename then you can recover the index with get(filename, default_value)

i=1
d = {}
for filename in filenames:
   if not d.get(filename, False):
       d[filename] = i
       i+=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM