简体   繁体   English

有没有办法避免在 Python 中包含引号的格式化字符串中使用双引号

[英]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.

相关问题 python-有没有一种聪明的方法来格式化带引号的字符串? - python - is there a intelligent way for formatting strings with quotations? 用Python字符串列表中的括号替换双引号 - Replace double quotations with brackets in a Python List of Strings 有没有办法忽略 python 中引号内的逗号? - Is there a way to ignore commas inside of quotation marks in python? 如何在 python 中的字符串列表上使用 zip 打印,而不返回周围的引号? - How do I use zip print on a list of strings in python without it returning with quotations surrounding them? 在python中格式化字符串内的整数和浮点数 - Formatting integers and floats inside strings in python 带双引号的熊猫查询 python - panda query with double quotation python Python双引号语法问题 - Python double quotation syntax issue Python:为什么列表导入使用引号,如何避免/摆脱它们? - Python: Why does list import using quotation marks, how can I avoid this/get rid of them? Python 3:使用多个相同的关键字格式化字符串,但使用唯一的字符串来替换它们 - Python 3: Formatting a string with multiple same keywords but unique strings to replace them 为什么我用双引号还是有单引号? - Why are there single quotations even though I am using double quotation marks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM