[英]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.我想创建一个必须采用以下格式的查询:'lat':'###','long':'###' 其中###s 代表纬度和经度。
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'"然而,结果将是一些类似的东西,它的开头和结尾都有“”:“'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?可笑的是,用 either.replace 或 .strip! 删除 " 是不可能的!并且将术语包裹在repr
周围并不能解决问题。你知道我如何去掉那些双引号吗?
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. PS 我知道当我打印命令时, "
不会显示,但是当我在查询中使用数组的每个元素时, "
将出现在查询的末尾,这会阻止它工作。 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.从查询的格式来看,我猜想它需要格式正确的 URL 参数: reverse_geocode.json?...
而'lat':'41.83','long':'-87.68'
不是。 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.假设您正在调用 twitter geo/ API,您可能想使用正确分隔的 URL 参数进行试验。
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])
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.