Python Forum
loop through csv format from weburl in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop through csv format from weburl in python
#1
what I need to do is , extract hostnames from the weburl which is in csx format , loop through it , extract the first column starting with xyz, assign it to a variable and use the variable in the python script to extract DNS Alias for the host.

format of the csv file is fields separated by delimiter ,


Quote:example : xyz.python.org,blah1,blah2,blah3
abc.python.org,some1,some2,some3
def.python.org,some1,some2,some3
xyz12.python.org,some1,some2,some3



here is my code:


         #!/usr/bin/python3
     
    import requests
    import json
    import os
     
    res = requests.get('https://myorg.csv')
     
    hostName = 'xyz.python.org'
     
    gm = "https://mygridmaster.org/"
    wapi = "wapi/v2.11/"
    ibobject = "record:host"
    
     
    gm_user = "myuser"
    gm_pwd ="mypwd"
     
    response = requests.get(gm + wapi + ibobject + '?_return_fields%2B=aliases&name=' + hostName,
                        auth=(gm_user, gm_pwd))
     
     
    members = response.json()
    for member in members:
         host = print (member['name'], end=' dns=')
         dnsAlias = print (member['aliases'], )
    
     
    
    print(res.text)
The code is working for me to get my output in following format

Output:
hostname dns=dnsalias
the last print statement is irrelavant here, as I had it to test if the csv can be read from this code. What I am unable to get to work is, extract the hostname start with xyz from the url and pass it as a variable to hostName. There are several hundred of hostnames in the file that I will need to loop through.


PS : I am unable to insert a code snippet for some reason.
Gribouillis write Jan-17-2024, 04:42 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
What are you getting back for members? Can you post a few lines of that and point out what parts in members you want to keep?
Reply
#3
(Jan-17-2024, 07:07 PM)deanhystad Wrote: What are you getting back for members? Can you post a few lines of that and point out what parts in members you want to keep?

Hi @deanhystad, thanks for the response.

members is not an issue. the issue is with how can I extract the hostname with certain prefix from the csv and pass it as a variable to my get command to WAPI api. I couldn't get a regex to work to extract it.
Reply
#4
I would like to see the format of the information returned by response.json(), so "members" is very much an issue.

You should not need a regex to extract anything. members will be a dictionary or a list or a list of dictionaries or something like that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through array items dynamically in a certain format bhojendra 3 2,657 Jun-11-2019, 03:37 AM
Last Post: micseydel
  Convert SAS Dates Format in a loop MohanReddy 2 3,058 Apr-02-2019, 10:31 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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