gpt4 book ai didi

python - 如何搜索关键字并打印文件,仅打印关键字中的行

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:47 25 4
gpt4 key购买 nike

我正在尝试读取文件并提取包含关键字的行,下面是该文件的示例:

    '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

这是我用来提取我想要的数据的代码

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

这就是我的结果。这里我需要清除字符串中 1CAM 之前的所有内容并明确排序,我需要这样的结果:

1CAM01Pffd8ffe000104a46494600010101006000600000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2036300affdb0043000d090a0b0a080d0b0a0b0e0e0d0f13201513121213271c1e17202e2931302e292d2c333a4a3e333646372c2d405741464c4e525352323e5a615a50604a51524fffdb0043010e0e0e131113261515264f352d354f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4fffc000110800f0014003012200021101031101ffc4001f0000010501010101010100000000', 1CAM02P000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b5110002010204040304070504040001027700010203',

所以,我知道我可以使用 [x:x] 清除星号或结束字符,但我的输入数据有时会在 1CAM header 之前添加或删除字符,这种方式不可靠,我需要用“1CAM”打开。

建议?

最佳答案

我已打印它而不是写入文件。

请检查

import re

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

for line in shakes:

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

输出

python rawcam.py
1CAM01Pffd8ffe000104a46494600010101006000600000fffe003b43524541544f523a2067642d6a7065672076312e3020287573696e6720494a47204a50454720763632292c207175616c697479203d2036300affdb0043000d090a0b0a080d0b0a0b0e0e0d0f13201513121213271c1e17202e2931302e292d2c333a4a3e333646372c2d405741464c4e525352323e5a615a50604a51524fffdb0043010e0e0e131113261515264f352d354f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4fffc000110800f0014003012200021101031101ffc4001f0000010501010101010100000000
1CAM02P000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b5110002010204040304070504040001027700010203
1CAM03P1104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e3e945252d4310e14b4dcd2e6900e14e14ccfbd2ee148448bd69d904107041ec6a0f3052f986857bdd06e6a586ab7ba767ec774f103d57ef29fc0d74da7f8e7055752b6e31cc9073cffbbffd7ae1b7134a09ed5b

关于python - 如何搜索关键字并打印文件,仅打印关键字中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59325612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com