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)

Quickbeam2k1
  • 5,287
  • 2
  • 26
  • 42
Phil
  • 103
  • 7
  • 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. – tda May 03 '18 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. – chepner May 03 '18 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.) – chepner May 03 '18 at 12:59
  • what you are expecting ? – Vikas Periyadath May 03 '18 at 13:00

3 Answers3

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
tda
  • 2,045
  • 1
  • 17
  • 42
  • 1
    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 rain = (rain["3h"]) KeyError: '3h' But thanks for getting me on the right track. – Phil May 03 '18 at 13:57
  • You can run rain.keys() to see what keys are in this dictionary, this may help to see what you can access inside :) – tda May 03 '18 at 14:05
  • rain = w.get_rain() # Get rain volume {'3h': 0} >>> print rain.keys() >>> Output : [] >>>> hmm, not very helpful. – Phil May 03 '18 at 15:26
  • 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. – tda May 03 '18 at 15:28
  • 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. – Phil May 11 '18 at 09:50
  • That makes sense. The above code only works if the dictionary is populated :-) – tda May 11 '18 at 12:37
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));
jspcal
  • 50,847
  • 7
  • 72
  • 76
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