So I'm having trouble getting my code to use a list of strings as inputs in a loop. Here's roughly what I have so far.
from arcgis.gis import GIS
Users = ['User01','User02','User03']
User_string = str(Users) # Have to do this as code needs input as string
gis = GIS("https://www.arcgis.com","USERNAME","PASSWORD") # This logs you into ArcGIS Online
User_role = 'org_user'
for x in User_string:
test = gis.users.get(username=x)
test.update_role(role=User_role)
print("Done! Check Web")
I just can't get the loop to work right. When I remove the for loop and put each user name in individually the get user and update role commands work just fine, it's just in the loop that is broken.
The two errors I'm getting is that the username has to be a string. I fixed that by adding the str() command, but I can't get the username to enter into the user.get loop.
Any suggestions? This code is actually looking at an excel file to produce the list of usernames so I can't just hardcode the list into the code. If it helps at all the website I've been using for the ArcGIS portion of the code is this one: https://developers.arcgis.com/python/guide/accessing-and-managing-users/
I should mention that I also tried just printing
test=gis.users.get(username=User_string)
And it came back as None. So I guess my question is how do I get 'User01' to go into the username=x spot?
Thanks much!
You're doing a for with the list as a string so it's looping on each character of the string. You need to do it with the original list.
Based on your code, you are telling your for loop to iterate on a String, since you converted your list to a string with User_string = str(Users); So the loop is going over each character on the string, which now it is User01User02User03
What you need to do is to iterate the list Users, like:
from arcgis.gis import GIS
Users = ['User01','User02','User03']
gis = GIS("https://www.arcgis.com","USERNAME","PASSWORD") # This logs you into ArcGIS Online
User_role = 'org_user'
for x in Users:
test = gis.users.get(username=x)
test.update_role(role=User_role)
print("Done! Check Web")
Related
I am trying to create a Twitter bot that posts a random line from a text file. I have gone as far as generating the random lines, which print one at a time, and giving the bot access to my Twitter app, but I can't for the life of me figure out how to use a printed line as a status.
I am using Tweepy. My understanding is that I need to use api.update_status(status=X), but I don't know what X needs to be for the status to match the most recently printed line.
This is the relevant section of what I have so far:
from random import choice
x = 1
while True:
file = open('quotes.txt')
content = file.read()
lines = content.splitlines()
print(choice(lines))
api.update_status(status=(choice(lines)))
time.sleep(3600)
The bot is accessing Twitter no problem. It is currently posting another random quote generated by (choice(lines)), but I'd like it to match what prints immediately before.
I may not fully understand your question, but from the very top, where it says, "How to use the most recently printed line as an input", I think I can answer that. Whenever you use the print() command, store the argument into a string variable that overwrites its last value. Then it saves the last printed value.
Instead of directly printing a choice:
print(choice(lines))
create a new variable and use it in your print() and your api.update_status():
selected_quote = choice(lines)
print(selected_quote)
api.update_status(status=selected_quote)
I have a tuple that I received after getting some data from an SQLite3 database and it looks like this (‘1’, ‘dextron’). How would I remove the brackets and add dextron to a string
After a bit of fiddling around i got it to work.
info = ("1','dextron')
user = info[1]
print(user)
Not sure if this is the best way to do it in python or not id like to hear any better ideas or imporvements
Sorry if this has been asked,I wasnt able to find it. I am building a slackbot and was looking to be able to loop through inputted data. The user would entered in IDs and the script would loop through those ids and return values. I am able to get it working if a single ID is entered but I was looking to have it search multiple IDs at once.
Entered in slack
#SlackBot search id1,id2,id3
I tried to enter the info from the chat into a list separated by a comma but python treats every character as a new asset in the list. (i,d,1, ,i,d,2,..)
I was able to have the data entered into a dictionary and when printed it shows as
[id1,id2,id3]
So i tried to loop through the dictionary but it treats that string as one object and doesnt loop.
def assetSearch(enteredID):
idList =[enteredID.upper()]
searchedIDs = list()
for eid in idList:
print(eid) # This is here to see what its looking at
for k, v in Content.items():
if v['AssetID'] == eid:
the current print(eid) prints [id1,id2,id3] instead of id1, then id2.
Could someone point me in the correct direction?
You need to do
idList = enteredID.upper().split(",")
As part of my project, I want to ask the user to enter an order from existing orders in a list.
So this is the problem: If my user writes the order in the input not exactly as its written in the list, how will my program understand it?
In other words, I want to turn this into Python code:
if (part of) string in order_list:
How can I do this?
order = input()
orders_list = ["initiate", "eat", "run", "set coords to"]
#Here is the problem
if order in orders_list:
#my output
For example, lets say I entered "eating" instead of "eat". How will my code
understand that I meant "eat"?
You can see if any of your words are contained in the users word like this:
if any(word in order for word in orders_list):
#my output
>>> from difflib import get_close_matches
>>> orders_list = ["initiate", "eat", "run", "set coords to"]
>>> get_close_matches('eating', orders_list)
['eat']
The fact that you are struggling with this should be an indication that your data structure is wrong.
Instead of getting the user to write a single string, get them to enter individual elements and store them separately in a list.
I need to extract an ID from a JSON string that is needed for loading information into a MySQL database. The ID is a 5 or 6 digit number, but the JSON key that contains this number is the URL net_devices resource string that has the number at the end like this example:
{u'router': u'https://www.somecompany.com/api/v2/routers/123456/'}
Since there is not a key with just the ID, I have used the following to return just the ID from the JSON key string:
url = 'https://www.somecompany.com/api/v2/net_devices/?fields=router,service_type'
r = json.loads(s.get((url), headers=headers).text)
status = r["data"]
for item in status:
type = item['service_type']
router_url = item['router']
router_id = router_url.replace("https://www.somecompany.com/api/v2/routers/", "")
id = router_id.replace("/", "")
print id
This does indeed return just the ID values I want, and it doesn't matter if the result varies in the number of digits.
The problem: This code creates an infinite loop when I include the two lines above the print statement.
How can I change the syntax to allow the loop to run through all the returned IDs once, but still strip out everything except the numerical ID?
I am new to Python, and just starting to write code again after a very long hiatus since college. Any help would be greatly appreciated!
UPDATE
Thanks everyone for the feedback! With the help from David and Gerrat, I was able to find the issue that was causing the infinite loop and it was not this segment of the code, but another segment that was not properly indented. I am learning how to properly indent loops in Python, and this was one of my silly mistakes! Thanks again for the help!