sepehr
sepehr

Reputation: 23

Is there a way to avoid double quotation in formatting strings which include quotations inside them in Python

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

Upvotes: 1

Views: 726

Answers (2)

Xiddoc
Xiddoc

Reputation: 3638

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])
})

Upvotes: 0

Alain T.
Alain T.

Reputation: 42129

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

Upvotes: 0

Related Questions