Python Forum
Extracting Specific Lines from text file based on content.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting Specific Lines from text file based on content.
#4
You did not specify you were writing a RESTful client. API is a really common term and I don't do a lot of web stuff. The json makes more sense now.

resp = restclient.get('API_Call_URI_here') returns a response, it does not write to the console. If something is being written to the console it is not because of the API call. How are you running this code? If you are running Python from the interactive window, the REPL automatically prints a reply to each command. For your command it would print resp.json. This is an artifact of how you are running the code and not real output.

resp will hold the response to your "get". resp.status_code is the status code for the request. resp.json is a json string with all the interesting stuff. You can convert the json string to a Python dictionary to make it easier to work with.
import requests
import json

API_ENDPOINT="https://MyURL.com"
 
restclient = requests.RestClient(API_ENDPOINT,
                credentials_file='MyCredentialsFile.json',
                verify=False)
 
resp = restclient.get('API_Call_URI_here')

data = json.loads(resp.json)
print(data["hosts_list"])
This should print:
Output:
[ { "host_name": "TestingHost", "port_number": 443 } ]
Using the dictionary you can ask for any of the json fields using their key name. These are the keys in the response you posted.
Quote:"LINE_1", "LINE_2", "EXAMPLE_3", "created_at", "deleted", "deleted_at", "delta_interval",
"description", "disable_backup"' "hosts_list", "id", "name", "password"' "scope_id", "type"
Are you familiar with Python dictionaries?
Reply


Messages In This Thread
RE: Extracting Specific Lines from text file based on content. - by deanhystad - Mar-25-2022, 09:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting the correct data from a CSV file S2G 6 217 Yesterday, 04:50 PM
Last Post: snippsat
  Copy Paste excel files based on the first letters of the file name Viento 2 559 Feb-07-2024, 12:24 PM
Last Post: Viento
  Extracting specific file from an archive tester_V 4 648 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Color a table cell based on specific text Creepy 11 2,366 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Split pdf in pypdf based upon file regex standenman 1 2,243 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,213 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 1,073 Dec-13-2022, 03:19 PM
Last Post: finndude
  How to remove footer from PDF when extracting to text jh67 3 5,478 Dec-13-2022, 06:52 AM
Last Post: DPaul
  reading content between a specific xml tag saisankalpj 1 915 Oct-31-2022, 01:37 PM
Last Post: wavic
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,349 Jul-27-2022, 12:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020