-1

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?

MikiBelavista
  • 2,470
  • 10
  • 48
  • 70

2 Answers2

1
d =json.loads(x.strip("\n") for x in data)

TypeError: the JSON object must be str, bytes or bytearray, not generator

data is a list but you're not passing data to json.loads()

you're passing the whole x.strip("\n") for x in data, that's the generator.

Use something like

for x in data:
  d = json.loads(x.strip("\n"))

or

  [ json.loads(x.strip("\n")) for x in data ]

The second isn't really that good as it' snot very readable and you create an array of all the jsons.

Daviid
  • 630
  • 4
  • 17
0

I finally solved it this way.

[line.rstrip() for line in open('ufw.txt').readlines()]

Output

['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', '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', 'Aug 14 10:58:34 mjbc-IdeaPad kernel: [ 5503.044850] [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=47928 DF PROTO=TCP SPT=53386 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0', 'Aug 14 10:58:57 mjbc-IdeaPad kernel: [ 5525.803159] [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=51243 DF PROTO=TCP SPT=34210 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0', 'Aug 14 10:59:12 mjbc-IdeaPad kernel: [ 5540.809454] [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=52935 DF PROTO=TCP SPT=49134 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0', 'Aug 14 10:59:34 mjbc-IdeaPad kernel: [ 5562.952636] [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=48582 DF PROTO=TCP SPT=52082 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0', 'Aug 14 10:59:52 mjbc-IdeaPad kernel: [ 5580.872876] [UFW BLOCK] IN=cni0 OUT= PHYSIN=vethf557eff2 MAC=4a:77:22:2f:99:e6:12:14:48:00:d5:3b:08:00 SRC=10.42.0.13 DST=192.168.1.183 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=59736 DF PROTO=TCP SPT=54094 DPT=6443 WINDOW=64860 RES=0x00 SYN URGP=0', 'Aug 14 11:00:12 mjbc-IdeaPad kernel: [ 5600.808743] [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=57060 DF PROTO=TCP SPT=35966 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0', 'Aug 14 11:00:34 mjbc-IdeaPad kernel: [ 5622.856630] [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=52788 DF PROTO=TCP SPT=40132 DPT=10250 WINDOW=64860 RES=0x00 SYN URGP=0']
MikiBelavista
  • 2,470
  • 10
  • 48
  • 70
  • 1
    So what you actually needed was a list of all logs, what you tried was `json.loads()`. An xy problem! – Standard_101 Aug 14 '23 at 11:14
  • @Standard_101Because I thought I could get it that way. – MikiBelavista Aug 14 '23 at 12:33
  • 1
    I understand that. What I meant was that the question could have been better asked: My goal is to get this and what I've tried is this! That way, the readers would have understood the question better. :) – Standard_101 Aug 14 '23 at 13:26