I have my logs file,first step was readlines
>>> with open('ufw.txt', 'r') as f:
... data = f.readlines()
works fine
data[0]
'Aug 14 10:57:53 mjbc-IdeaPad kernel: [ 5462.345134] [UFW BLOCK] IN=cni0 OUT= PHYSIN=vethea71b2e5 MAC=4a:77:22:2f:99:e6:2e:56:aa:98:1c:2a:02:04 SRC=10.42.0.11 DST=192.168.1.183 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=26281 DF PROTO=TCP SPT=57846 DPT=6443 WINDOW=64860 RES=0x00 SYN URGP=0 \n'
then
data[1]
'Aug 14 10:58:12 mjbc-IdeaPad kernel: [ 5480.804967] [UFW BLOCK] IN=cni0 OUT= PHYSIN=vetha892c162 MAC=4a:77:22:2f:99:e6:42:f2:90:55:d9:af:08:00 SRC=10.42.0.4 DST=192.168.1.183 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=33147 DF PROTO=TCP SPT=60154 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0 \n'
When I go for
d =json.loads(x.strip("\n") for x in data)
I got
TypeError: the JSON object must be str, bytes or bytearray, not generator
type(data)
<class 'list'>
I tried what Daviid suggested,
[ json.loads(x.strip("\n")) for x in data ]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> for x in data:
... d = json.loads(x.strip("\n"))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How to get rid of /n?