Python Forum

Full Version: Please Need help with python script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello


I have2 python scripts that I need to combine to read a csv file line by line and take loop through the data in the csv exporting it via cURL to one POST and 2 PUT the main script is below. Right now in the script I have hardcoded in email address, employee ID and password (bold and underlined) (these are the items I need to extract from a csv and then loop back through the csv after it cURL through the POST, PUT, PUT


Any help would be greatly appreciated as I am a sys admin and not a programmer but I am on a deadline to get this completed


import sys import requests url = 'https://domain.com/iredadmin/api' # Admin email address and password. admin = '[email protected]' pw = 'domainpassword' # Login r = requests.post(url + '/login', data={'username': admin, 'password': pw}) # Get returned JSON data data = r.json() if not data['_success']: sys.exit('Login failed') cookies = r.cookies # Create user: <user>@domain.com # (url + api endpoint , data = parameters # hard coded preferred language & mail quota requests.post(url + '/user/[b][email protected][/b][u][/u]', cookies=cookies, data={'cn': 'My Name', 'password': '[b]1@Password12345[/b][u][/u]', 'preferredLanguage': 'en_US', 'quota': '5120'}) # Update user: <user>@domain.com requests.put(url + '/user/[b][email protected][/b][u][/u]', cookies=cookies, data={'cn': 'My New Name', 'employeeid' : '[b]testID[/b][u][/u]', 'language': 'en_US', }) requests.put(url + '/ml/[email protected]', cookies=cookies, data= {'add_subscribers': '[b][email protected][/b][u][/u]', })
THIS IS THE SECOND SCRIPT THAT READS THE CSV

import csv input_file = csv.DictReader(open("users.csv")) for row in input_file: plantid = str(row["plant number"]) passwd = str(row["password"]) emailaddrs = str(row["email address"]) print(plantid) print(passwd) print(emailaddrs)
It would help you out a lot if you use functions.
Used properly, it will make your scripy importable.
Then you can import your csv reader wherever needed.
example (untested):
import csv # read_csv generator def read_csv(filename): with open(filename) as fp: cdrdr = csv.Reader(fp) for row in cdrdr: yield(row) def testit(): for row = read_csv("users.csv"): # add field index between [] plantid = row[] passwd = row[] emailaddrs = row[] print(plantid) print(passwd) print(emailaddrs) if __name__ == '__main__': testit()
now you can import this script for example if named MyCsvReader.py then
import into other module with import MyCsvReader
Thanks for the help, I am able to add this into my main script as you suggested by importing it... my next problem, is getting the main script to read the output and curl it (POST, PUT, PUT) and loop through it one row at a time... again I am sorry for my ignorance here the last time I even worked w/ python was ~10 years ago in undergrad as a requirement.
you read the data same as in testit with one exception.
precede call to read_csv with MyCsvReader.read_csv
If you have a different name for the script, then use that name for the import, and the prefix