1

It is not supposed to be a hard problem, but I've worked on it for almost a day!

I want to create a query which has to be in this format: 'lat':'###','long':'###' where ###s represent latitude and longitude.

I am using the following code to generate the queries:

coordinateslist=[]
for i in range(len(lat)):
     coordinateslist.append("'lat':'{}','long':'-{}'".format(lat[i],lon[i]))
coordinateslist

However the result would be some thing similar to this which has "" at the beginning and end of it: "'lat':'40.66','long':'-73.93'"

Ridiculously enough it's impossible to remove the " with either .replace or .strip! and wrapping the terms around repr doesn't solve the issue. Do you know how I can get rid of those double quotation marks?

P.S. I know that when I print the command the "will not be shown but when i use each element of the array in my query, a " will appear at the end of the query which stops it from working. directly writing the line like this:

query_params = {'lat':'43.57','long':'-116.56'}

works perfectly fine.

but using either of the codes below will lead to an error.

aa=print(coordinateslist[0])
bb=coordinateslist[0]

query_params = {aa}
query_params = {bb}
query_params = aa
query_params = bb
FountainTree
  • 316
  • 1
  • 11
sepehr
  • 23
  • 6
  • 3
    Those quotes are not actually there, they are the representation of the string. If you try to print the string to the screen (e.g., `print(coordinateslist[0])`), you will notice that there are no `" "` around the string. – Xiddoc Sep 30 '21 at 18:11
  • Thanks for your reply, when I use the code you mentioned in my query i get the error 'message': 'Invalid coordinates.' and when i directly use coordinateslist[0] in the query, i see " appearing in the query like this: reverse_geocode.json?'lat':'41.83','long':'-87.68'", – sepehr Sep 30 '21 at 18:18
  • Again, those `"` are just to tell you "this object is a string". They are not actually there. When you access strings directly, Python will put the `"` around them to inform you that the type of this object is a string. When your code accesses the object, the `"` is not there. Again, for example, try to `print(coordinateslist[0])`. You will see that there are no `"` in the output. – Xiddoc Sep 30 '21 at 18:27
  • Please see the p.s of the post i just added. – sepehr Sep 30 '21 at 18:34
  • 1
    Maybe you meant to do `coordinateslist.append({"lat": lat[i], "long": "-{}".format(lon[i])})`? – Xiddoc Sep 30 '21 at 18:40
  • 1
    You probably shouldn't be making a list of strings. Make a list of dictionaries and convert it to JSON. – Barmar Sep 30 '21 at 18:40
  • @Barmar thanks, i had not thought about that. – sepehr Oct 01 '21 at 07:13
  • @sepehr No problem, I added my answer onto this post. If I helped, please upvote it and mark it as valid using the checkmark :) – Xiddoc Oct 01 '21 at 07:17

2 Answers2

0

It is likely that the error you are getting is something else entirely (i.e. unrelated to the quotes you are seeing in printed outputs). From the format of the query, I would guess that it is expecting a properly formatted URL parameters: reverse_geocode.json?... which 'lat':'41.83','long':'-87.68' is not. Did you try to manually call it with a fixed string, (e.g. using with postman) ?

Assuming you're calling the twitter geo/ API, you might want to ry it out with properly separated URL parameters.

geo/reverse_geocode.json?lat=41.83&long=-87.68
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Yes i am trying to call twitter API bur as i have a lot of coordinates i have to automate it. Thanks for your comment, i used the method @Xiddoc suggested :) – sepehr Oct 01 '21 at 07:15
0

Try using a dictionary instead, if you don't want to see the " from string representation:

coordinateslist.append({
    "lat": lat[i],
    "long": "-{}".format(lon[i])
})
Xiddoc
  • 3,369
  • 3
  • 11
  • 37