Python Forum

Full Version: loop through csv format from weburl in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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?
(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.
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.