简体   繁体   中英

Parsing through text file in Python and printing the line matching the String

I have a file named file.txt which contains some text. There is an another file details.txt containing the strings to be fetched from file.txt and print the line matching the strings from details.txt.

files.txt

12345 04/04/19 07:06:55  entered | computer message|  ID WRE435TW: headway | | 
23456 04/04/19 07:10:00  entered | computer message|  Double vehicle logon  | | 
23567 04/04/19 07:06:55  entered | computer message|  ID EWFRSDE3: small   | | 
09872 04/04/19 07:07:47  entered | computer message|  Double vehicle logon  | | 
76789 04/04/19 07:10:05  entered | computer message|  Veh : logoff          | | 

details.txt

headway
small
logoff
logon

I Tried parsing the text file but not not getting the proper formatted output.

import pandas as pd
import re
import os
import glob
import csv


os.chdir("file_path")

with open("file.txt", "r") as fp:
    with open("details.txt", 'r+') as f:
        for i in f:
            for line in fp:
                if i:
                    print(i)
                else:
                    print('no Event')

Just use Pandas POWER:

import pandas as pd
import numpy as np

# Read the file as CSV with custom delimiter
df = pd.read_csv(
    'files.txt',
    delimiter='|',
    header=None
)

We will get it:


    0                                   1                   2                     3 4
0   64834 04/04/19 07:06:55 entered     computer message    Veh SBS3797R: headway       
1   73720 04/04/19 07:10:00 entered     computer message    Double vehicle logon        
2   64840 04/04/19 07:06:55 entered     computer message    Veh SBS3755L: small         
3   67527 04/04/19 07:07:47 entered     computer message    Double vehicle logon        
4   73895 04/04/19 07:10:05 entered     computer message    Veh : logoff        

Pick the third column (with index 2) and transform it:

words = np.vectorize(lambda x: x.strip().split(' ')[-1])(df[2].values)

np.vectorize applies the function lambda x: x.strip().split(' ')[-1] (clean the text and pick the last word) to the third column df[2].values

So you can write it to the result file:

with open("details.txt", 'a+') as f:
    f.write('\n'.join(words))

Note, that you should use a+ to append to the result file. r prohibit to do it.

Notice that string different than '' in python is consider as True . So in your code:

with open("file.txt", "r") as fp:
    with open("details.txt", 'r+') as f:
        for i in f:
            for line in fp:
                if i:  # This is always true (for input you showed)
                    print(i)
                else:
                    print('no Event')

You could try this:

with open("file.txt", "r") as fp:
    with open("details.txt", 'r+') as f:
        for i in f:
            for line in fp:
                if i in line:  
                    print(line)  # I assume you wanted to print line from files.txt
                else:
                    print('no Event')

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