Updating Dictionaries in Python
I am trying to have my dictionary continuously update for as long as the loop is running, with the x and y values changing as needed. This is how I initially tried 开发者_Python百科it before getting the error message and coming here.
params = urllib.urlencode({'name':'xxxxx', 'pass':'xxxxxxx', 'amount':'x', 'price':'y'})
x = math.floor(first)
y = last*1.007-last*.003
params['amount'] = x
params['price'] = y`
sell = urllib.urlopen("https://sellyourstuffwhatever.com", params)
I don't know a whole lot about Python, so I'm sure there is a way to do this. The current method however gives me this error.
"TypeError: 'str' object does not support item assignment"
Edit: So I need to be able to update my price and amount every thirty minutes or so, which would be done automatically as the script is looping. The website requires my username, password, price and amount. The username and password never change, however the price and amount do. So is there anyway that I can continuously update them?
urllib.urlencode
[docs] returns a string, not a dictionary. You have to call it after your loop.
Something like:
params = {'name':'xxxxx', 'pass':'xxxxxxx'}
for .....:
params['amount'] = math.floor(first)
params['price'] = last*1.007-last*.003
params = urllib.urlencode(params)
Even better if you use a different variable name for the encoded string.
If you don't know much about Python yet, have a look at some tutorial.
urllib.urlencode returns a string. As strings are immutable objects, you cannot assign a value to a part of it, hence the error.
Anyhow: from your code it seems that you would expect param
to be a dictionary. Maybe you should clarify your question a bit more...
精彩评论