简体   繁体   中英

How do I search a keyword and print a file with only print the lines from the keyword on

I'm tryig to read a file and extract the lines that contain keywords, below is an example of the file:

    'mId': 67768924,
    'dtHr': '12/12/2019 11:26:25',
    'dados': b'1CAM01Pffd8ffe000104a46494600010101006000600000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2036300affdb0043000d090a0b0a080d0b0a0b0e0e0d0f13201513121213271c1e17202e2931302e292d2c333a4a3e333646372c2d405741464c4e525352323e5a615a50604a51524fffdb0043010e0e0e131113261515264f352d354f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4fffc000110800f0014003012200021101031101ffc4001f0000010501010101010100000000',
    'isn': '01057818SKY0ABF',
    'tipo': 0,
    'bytes': 0
}, {
    'mId': 67768940,
    'dtHr': '12/12/2019 11:26:47',
    'dados': b'\r\n1CAM02P000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b5110002010204040304070504040001027700010203',
    'isn': '01057818SKY0ABF',
    'tipo': 0,
    'bytes': 0
}, {
    'mId': 67768952,
    'dtHr': '12/12/2019 11:27:01',
    'dados': b'\r\n1CAM03P1104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e3e945252d4310e14b4dcd2e6900e14e14ccfbd2ee148448bd69d904107041ec6a0f3052f986857bdd06e6a586ab7ba767ec774f103d57ef29fc0d74da7f8e7055752b6e31cc9073cffbbffd7ae1b7134a09ed5b',
    'isn': '01057818SKY0ABF',
    'tipo': 0,
    'bytes': 0

That's the code I'm using to extract the data I want

import re

shakes = open("rawcam.txt", "r")

for line in shakes:

    if re.match("(.*)1CAM(.*)", line):
        f = open('cam1read.txt', 'a+')
        f.write(line)  
        f.close() 
'dados': b'1CAM01Pffd8ffe000104a46494600010101006000600000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2036300affdb0043000d090a0b0a080d0b0a0b0e0e0d0f13201513121213271c1e17202e2931302e292d2c333a4a3e333646372c2d405741464c4e525352323e5a615a50604a51524fffdb0043010e0e0e131113261515264f352d354f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4fffc000110800f0014003012200021101031101ffc4001f0000010501010101010100000000',
'dados': b'\r\n1CAM02P000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b5110002010204040304070504040001027700010203',

That's my result. here I need to clear everything that comes before 1CAM on the string and get it clear for the sorting, and I need a result like this:

1CAM01Pffd8ffe000104a46494600010101006000600000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2036300affdb0043000d090a0b0a080d0b0a0b0e0e0d0f13201513121213271c1e17202e2931302e292d2c333a4a3e333646372c2d405741464c4e525352323e5a615a50604a51524fffdb0043010e0e0e131113261515264f352d354f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4fffc000110800f0014003012200021101031101ffc4001f0000010501010101010100000000', 1CAM02P000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51 10002010204040304070504040001027700010203',

So, I know I could use [x:x] to clear star or end characters, but my input data sometimes add or remove characters before the 1CAM header and this way isn't reliable, I need to write the lines with the '1CAM' on.

Suggestions?

I've printed it instead of writing to a file.

Please check it

import re

shakes = open("rawcam.txt", "r")

for line in shakes:

    m = re.match(".*(1CAM[^'\"]+)", line)
    if m:
        print(m.group(1))

Ouput

python rawcam.py
1CAM01Pffd8ffe000104a46494600010101006000600000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2036300affdb0043000d090a0b0a080d0b0a0b0e0e0d0f13201513121213271c1e17202e2931302e292d2c333a4a3e333646372c2d405741464c4e525352323e5a615a50604a51524fffdb0043010e0e0e131113261515264f352d354f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4fffc000110800f0014003012200021101031101ffc4001f0000010501010101010100000000
1CAM02P000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b5110002010204040304070504040001027700010203
1CAM03P1104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e3e945252d4310e14b4dcd2e6900e14e14ccfbd2ee148448bd69d904107041ec6a0f3052f986857bdd06e6a586ab7ba767ec774f103d57ef29fc0d74da7f8e7055752b6e31cc9073cffbbffd7ae1b7134a09ed5b

Thanks Alexandr, I took your code and pieced together what I was missing to get the output I wanted, I'll post it here in case anyone ever need it. It saves a txt file, from the argument on with the datetime.

import re
import datetime

shakes = open("rawcam.txt", "r")
filename = datetime.datetime.now()

for line in shakes:

m = re.match(".*(1CAM[^'\"]+)", line)
if m:
    print((m.group(1)),line[0:-2], file=open(filename.strftime("%d %B %Y")+"1CAMRAW.txt", "a+"))

Once again, thanks a bunch

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