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.
#1
Hey, all. My apologies as I'm new to python in general, so I'm doing a lot of reading that goes over my head. :)

I'm working on a multi-step process where I run one python script that makes an API call to a system. I then have to extract some of those values from that output so that I can submit them, along with a new password, via a second API call that I would make after.

I have two main questions, first, is it possible to pipe the output of the initial API call directly to a text file rather than just to the console?

Second, is it even possible for python to look through a text file and extract certain lines based on the values in that line?

Some sample output from the first API call:
Output:
{ "LINE_1": true, "LINE_2": "Post \"https://a:443/sdk\": dial tcp: lookup a on 1.1.127.252:53: no such host", "EXAMPLE_3": "", "created_at": 1648040001, "deleted": false, "deleted_at": 0, "delta_interval": 60, "description": "API's Testing.", "disable_backup": false, "hosts_list": [ { "host_name": "TestingHost", "port_number": 443 } ], "id": "623b1841755f021ebe269xyz", "name": "Tester", "password": "", "scope_id": "5f7e2a15497d4f48a9605687", "type": "vcenter", }
There are four specific lines I am trying to extract:
"name":
"type":
"hosts_list":
"username":

I've come across some things describing how to convert lines of a text file into individual objects, but that only allows me to print lines based on the number line they are in a text file, it doesn't print lines based on the content in their lines.

Also, if this is possible, the "hosts_list" line may cause an issue because it actually has a subsection that can contain multiple entries. In the above example the subsection is:
Output:
{ "host_name": "TestingHost", "port_number": 443 }
Thank you for any help you can provide.
Reply


Messages In This Thread
Extracting Specific Lines from text file based on content. - by jokerfmj - Mar-25-2022, 12:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 493 Feb-07-2024, 12:24 PM
Last Post: Viento
  Extracting specific file from an archive tester_V 4 571 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Color a table cell based on specific text Creepy 11 2,136 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Split pdf in pypdf based upon file regex standenman 1 2,151 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,166 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 1,022 Dec-13-2022, 03:19 PM
Last Post: finndude
  How to remove footer from PDF when extracting to text jh67 3 5,241 Dec-13-2022, 06:52 AM
Last Post: DPaul
  reading content between a specific xml tag saisankalpj 1 881 Oct-31-2022, 01:37 PM
Last Post: wavic
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,302 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Delete multiple lines from txt file Lky 6 2,373 Jul-10-2022, 12:09 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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