0

I've been using the weather site Open Weather Map (https://openweathermap.org/) to get details for a project in Python.

rain = w.get_rain() # Get rain volume {'3h': 0} 
wind = w.get_wind() # Get wind degree and speed {'deg': 59, 'speed': 2.660}
humidity = w.get_humidity() # Get humidity percentage 67

And then insert them the variables into a database, which doesn't like '{' '}'.

I've tried removing the '{' '}' from the outputs, looking at replies from this site. Strip:

rain = rain.strip(['{','}'])

and Replace:

rain = rain.replace('{', ' ')

But all I'm getting are "AttributeError: 'dict' object has no attribute". Is there another escape clause I could use for the variable to remove the unwanted characters?

How to use string.replace() in python 3.x

How to remove certain characters from a variable? (Python)

4
  • 1
    A python dictionary is returned and you need to call the key and value instead of removing the {}. I'll add an answer that shows how to do this. Commented May 3, 2018 at 12:57
  • There are a few problems here, and it's not clear which on you want to address. You are getting back JSON from the API, so you probably want to use the json module to work with the responses. Further, you should be using your database API to deal with special characters if you really want to store the entire JSON result, not just an extracted value, rather than trying to manually escape things. Commented May 3, 2018 at 12:57
  • (Actually, the AttributeError comes from the fact that you do have a dict, not a JSON object, which raises the question of what you mean by the database not liking the braces.) Commented May 3, 2018 at 12:59
  • what you are expecting ? Commented May 3, 2018 at 13:00

3 Answers 3

3

A python dictionary is returned and you need to access the key and value instead of removing the {}. The following shows how to access dictionary values.

print(rain.keys()) # returns a list of all dictionary keys
>>> ['3h']

print(rain['3h']) # returns the value associated with the '3h' key
>>> 0
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, that really helped. I was looking at the {}, rather than what was inside them. While it's worked for all the other keys, '3h' doesn't appear to actually be the key: File "weather.py", line 63, in <module> rain = (rain["3h"]) KeyError: '3h' But thanks for getting me on the right track.
You can run rain.keys() to see what keys are in this dictionary, this may help to see what you can access inside :)
rain = w.get_rain() # Get rain volume {'3h': 0} >>> print rain.keys() >>> Output : [] >>>> hmm, not very helpful.
Can you print "w.get_rain()" and "type(w.get_rain())"? If the first one returns a dictionary, there should be no reason why the .keys() method returns blank.
Having Tweeted OWN, they say "In the JSON response the rain value is not provided. If it is measured - it is returned in the API response" So, if it's raining you get a response, it it's not you get {}, which screws up the SQL insert. I'm going to try a couple other things, but it's a not a huge loss to my project not having it for the moment.
|
0

Since your variable is a dict, you'll need to create a suitable string representation of it. You can concatenate the keys and values with your own formatting like so:

# a: 1, b: 2, c: 3
myString = ', '.join('{}: {}'.format(k, v) for k,v in myDict.items())

Or modify the default string representation returned by str

# 'a': '1', 'b': '2', 'c': '3'
myString = re.sub('[{}]', '', str(myDict));

Comments

0

first of all you need to know what type of data you get:

print (type(obj))

and this is not a string, you can't replace any symbols.

one more. if you get info from site as like json-object, so you don't need replace anything because you can use key for parsing info and, if you need, write to database

2 Comments

The returned error is "AttributeError: 'dict' object has no attribute" so we know it's a dictionary.
and if this is a dictionary, we can write "if 'rain' in obj: rain = obj['rain'] else: rain = ' ' "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.