Reputation: 29
I'm trying to read some data from a text file I created into a list, but keep getting the "TypeError: 'str' object is not callable" error when i try to do this.
Here is the code
class Weather():
"""
Weather Class
"""
def __init__(self,weather = ''):
self.weather= weather
def rome_weather(self, weather =''):
"""
City temperature function
"""
#retrieving geocoding API to find longitude and latitude
api = '66c622a1d43cb6e0010946bc5408b773'
geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q=Rome,IT&limit=2&appid={api}"
request = urllib.request.urlopen(geo_url)
result = json.loads(request.read())
#defining longitude and latititude
for value in result:
for key in value:
lon = value["lon"]
lat = value["lat"]
#retrieving current weather API
weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api}"
request2 = urllib.request.urlopen(weather_url)
result2 = json.loads(request2.read())
#temperature variable
weather = result2['main']['temp'] - 273.15
weather = round(weather,2)
return f"{weather}\n"
weather= Weather()
for value in range(200):
with open ('romeweather.txt','a') as f:
f.write(str(weather.rome_weather(weather)))
rome=[]
with open ('romeweather.txt','r') as f:
for line in f:
if len(rome) <= 200:
rome.append(float(line()))
A class is made with a function that calls the temperature of Rome, from an API. This info is then stored in a text file "romeweather.text". I need to store 200 lines of the data from that file into a list. The list part isnt working for me because the data is in the form of floats ex.15.67 on each line. I keep running into this error:
Traceback (most recent call last):
File "main.py", line 111, in <module>
rome.append(float(line()))
TypeError: 'str' object is not callable
Please let me know how I can make it work.
Upvotes: 0
Views: 77
Reputation: 52
As has been previously said (and read by the error) you're trying to call a string, which is not callable. Simply remove the brackets on 'line()' and it will solve your problem.
rome.append(float(line))
Upvotes: 1
Reputation: 406
Line 111 should be as follows.
rome.append(float(line))
The problem was the ()
brackets that was causing python to try and call line
when it was in fact a string.
Upvotes: 1