Extract only certain text which are needed
Extract only certain text which are needed
|
Just as info so is data shown already a dictionary,so no need to put trough the Json module
I guess in code that you don't show that have been using Requests to get Json data. Request has build in Json decoder that give a Python dictionary back. So in my post i did nothing else than a plain copy of your data(and format it) the use assign it to a variable data .>>> type(data) <class 'dict'> >>> >>> print(f"amount: {data['_source']['amount']}, email: {data['_source']['email']}, mobile: {data['_source']['mobile']}, accountOwner: {data['_source']['accountOwner']}") amount: 300, email: [email protected], mobile: 100000012457, accountOwner: Tom HankSo no Json module is used and the f-string for ibreeden code work on it.
Oct-10-2022, 03:41 AM
(Oct-09-2022, 09:41 AM)ibreeden Wrote:(Oct-09-2022, 07:39 AM)rob101 Wrote: Okay, so the jason file has yet to be translated; I'll work on that.I think there is nothing wrong with the json. You should use the json module for the translating. This is the error I am getting Traceback (most recent call last): File "/media/redhat/test/Shodan/data/df.py", line 4, in <module> data = json.load(f) File "/usr/lib/python3.10/json/__init__.py", line 293, in load return loads(fp.read(), 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 340, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 1289) (Oct-09-2022, 07:39 AM)rob101 Wrote: Okay, so the jason file has yet to be translated; I'll work on that. In three million data it prints out only one line of data It looks like it's printing out the last line of the file
Oct-10-2022, 06:58 AM
(Oct-10-2022, 03:41 AM)Calli Wrote:This means the json file is corrupt. Open the json file and look at the beginning of line 2. Count the number of brackets, braces, parentheses (there should be an even number) and check for the comma's on the right places.
Oct-10-2022, 07:06 AM
(Oct-10-2022, 03:55 AM)Calli Wrote: It looks like it's printing out the last line of the file (Oct-10-2022, 07:06 AM)ibreeden Wrote: Is'nt that what you wanted? You want more? Then add 4 spaces before the print function. Indeed: move the print() function into the for loop.To add.. Either that, or provide a bigger sample (say 10 records) and I'll sort it out: reading (what was it... looking back) ... there we go, 3,000,000 records into memory is not good practice, as we can do this one record at time.
Sig:
>>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein (Oct-10-2022, 07:58 AM)rob101 Wrote:(Oct-10-2022, 03:55 AM)Calli Wrote: It looks like it's printing out the last line of the file Traceback (most recent call last): File "/media/redhat/test/data/df.py", line 15, in <module> print(amount,email,mobile,accountOwner) NameError: name 'amount' is not defined with open ('df.json', 'r') as f: content = f.read() temp = content.split(',') for item in temp: if 'amount' in item: amount = item.strip() elif 'email' in item: email = item.strip() elif 'mobile' in item: mobile = item.strip() elif 'accountOwner' in item: accountOwner = item.strip() print(amount,email,mobile,accountOwner)
Oct-10-2022, 08:07 AM
(Oct-10-2022, 08:02 AM)Calli Wrote: NameError: name 'amount' is not defined See my updated post (above).
Sig:
>>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Oct-10-2022, 08:07 AM
|
|
Users browsing this thread: