简体   繁体   中英

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?

PS 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

It is likely that the error you are getting is something else entirely (ie 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, (eg 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM