python strings要请求,为什么用\ var \ \&#x27代替双引号?
我正在与 valhalla 检索旅行持续时间。在文档中,发送请求的格式如下:
{"sources":[{"lat":40.744014,"lon":-73.990508}],"targets":[{"lat":40.744014,"lon":-73.990508},{"lat":40.739735,"lon":-73.979713},{"lat":40.752522,"lon":-73.985015},{"lat":40.750117,"lon":-73.983704},{"lat":40.750552,"lon":-73.993519}],"costing":"pedestrian"}&id=ManyToMany_NYC_work_dinner
我正在独立创建源和目标,这是来源的代码:
'{"sources":[{"lat": 19.690742994833027, "lon": -99.1680363639107]'
目标来自大熊猫的系列:
<style type="text/css">
.tg {
border-collapse: collapse;
border-spacing: 0;
}
.tg td {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
}
.tg th {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
font-weight: normal;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
}
.tg .tg-0pky {
border-color: inherit;
text-align: left;
vertical-align: top
}
.tg .tg-0lax {
text-align: left;
vertical-align: top
}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 433px">
<colgroup>
<col style="width: 91px">
<col style="width: 342px">
</colgroup>
<thead>
<tr>
<th class="tg-0pky">Reference</th>
<th class="tg-0pky">order_point</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky">1</td>
<td class="tg-0pky">[19.690742994833027,-99.1680363639107]</td>
</tr>
<tr>
<td class="tg-0lax">2</td>
<td class="tg-0lax">[19.67109, -99.16845]</td>
</tr>
<tr>
<td class="tg-0lax">3</td>
<td class="tg-0lax">[19.684034594944944, -99.15103970155211]</td>
</tr>
<tr>
<td class="tg-0lax">4</td>
<td class="tg-0lax">[19.686485, -99.23003]</td>
</tr>
<tr>
<td class="tg-0lax">5</td>
<td class="tg-0lax">[19.718996, -99.13548]</td>
</tr>
</tbody>
</table>
And are turned into a list of dicts:trgt_dicts = [{"lat": i[0], "lon": i[1]} for i in targets]
返回:
[{'lat': 19.690742994833027, 'lon': -99.1680363639107}, {'lat': 19.67109, 'lon': -99.16845}, {'lat': 19.684034594944944, 'lon': -99.15103970155211}, {'lat': 19.686485, 'lon': -99.23003}, {'lat': 19.718996, 'lon': -99.13548}]
所以,当我将所有东西放在一起时:
query = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets": 'f'{trgt_dicts}'', "costing":"auto"}'
我得到的是:
'{"sources":[{"lat": 19.690742994833027, "lon": -99.1680363639107], "targets": [{\'lat\': 19.690742994833027, \'lon\': -99.1680363639107}, {\'lat\': 19.67109, \'lon\': -99.16845}, {\'lat\': 19.684034594944944, \'lon\': -99.15103970155211}, {\'lat\': 19.686485, \'lon\': -99.23003}, {\'lat\': 19.718996, \'lon\': -99.13548}], "costing":"auto"}'
检索和错误,我认为原因是 \'lon \':我得到了而不是“ lon”:
我尝试使用json.dumps,以尝试获得其他问题中建议的正确格式。我还为源和目标创建了变量,并在使用F弦之后将它们放在一起:
qsource = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets":'
qtrgt = ','.join(map(str, [trgt_dicts]))
query = qsource + qtrgt + ',"costing":"auto"}'
但是最后,无论如何,我最终都得到了这个 \'lon \':
I'm working with Valhalla to retrieve trips durations. In the documentation the format to send the request is as follows:
{"sources":[{"lat":40.744014,"lon":-73.990508}],"targets":[{"lat":40.744014,"lon":-73.990508},{"lat":40.739735,"lon":-73.979713},{"lat":40.752522,"lon":-73.985015},{"lat":40.750117,"lon":-73.983704},{"lat":40.750552,"lon":-73.993519}],"costing":"pedestrian"}&id=ManyToMany_NYC_work_dinner
I'm creating the source and the targets independently, this is the code for the source:
'{"sources":[{"lat": 19.690742994833027, "lon": -99.1680363639107]'
The targets come from a Pandas' series:
<style type="text/css">
.tg {
border-collapse: collapse;
border-spacing: 0;
}
.tg td {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
}
.tg th {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
font-weight: normal;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
}
.tg .tg-0pky {
border-color: inherit;
text-align: left;
vertical-align: top
}
.tg .tg-0lax {
text-align: left;
vertical-align: top
}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 433px">
<colgroup>
<col style="width: 91px">
<col style="width: 342px">
</colgroup>
<thead>
<tr>
<th class="tg-0pky">Reference</th>
<th class="tg-0pky">order_point</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky">1</td>
<td class="tg-0pky">[19.690742994833027,-99.1680363639107]</td>
</tr>
<tr>
<td class="tg-0lax">2</td>
<td class="tg-0lax">[19.67109, -99.16845]</td>
</tr>
<tr>
<td class="tg-0lax">3</td>
<td class="tg-0lax">[19.684034594944944, -99.15103970155211]</td>
</tr>
<tr>
<td class="tg-0lax">4</td>
<td class="tg-0lax">[19.686485, -99.23003]</td>
</tr>
<tr>
<td class="tg-0lax">5</td>
<td class="tg-0lax">[19.718996, -99.13548]</td>
</tr>
</tbody>
</table>
And are turned into a list of dicts:
trgt_dicts = [{"lat": i[0], "lon": i[1]} for i in targets]
Which returns:
[{'lat': 19.690742994833027, 'lon': -99.1680363639107}, {'lat': 19.67109, 'lon': -99.16845}, {'lat': 19.684034594944944, 'lon': -99.15103970155211}, {'lat': 19.686485, 'lon': -99.23003}, {'lat': 19.718996, 'lon': -99.13548}]
So, when I put everything together:
query = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets": 'f'{trgt_dicts}'', "costing":"auto"}'
What I get is this:
'{"sources":[{"lat": 19.690742994833027, "lon": -99.1680363639107], "targets": [{\'lat\': 19.690742994833027, \'lon\': -99.1680363639107}, {\'lat\': 19.67109, \'lon\': -99.16845}, {\'lat\': 19.684034594944944, \'lon\': -99.15103970155211}, {\'lat\': 19.686485, \'lon\': -99.23003}, {\'lat\': 19.718996, \'lon\': -99.13548}], "costing":"auto"}'
Which retrieves and error, and I think the reason is the \'lon\': I'm getting instead of "lon":
I have tried with json.dumps in an attempt to get the right format as suggested in other questions. I also created variables for the source and the targets and put them together after playing around with the f-string:
qsource = '{"sources":[{"lat": 'f'{source[0]}'', "lon": 'f'{source[1]}''], "targets":'
qtrgt = ','.join(map(str, [trgt_dicts]))
query = qsource + qtrgt + ',"costing":"auto"}'
But at the end, no matter what, I end up with this \'lon\':
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
i think 您的意思是
查询
是字典,但是您正在创建一个字符串(通过附加一堆子字符串)。仔细观察您的语法:如果我们将子字符串拆分以使其更清楚,这就是您所写的:
老实说,这很奇怪。我认为你想要什么
喜欢:
那给你一个python词典。如果您想json对它进行编码,您
可以使用
json.dumps
。假设您有:这给了您:
I think you mean for
query
to be a dictionary, but you're creating a string (by appending a bunch of substrings). Take a closer look at your syntax:If we split up the substrings to make it more clear, this is what you've written:
And honestly, that's just weird. I think what you want is something
like:
That gives you a Python dictionary. If you want to JSON-encode it, you
can use
json.dumps
. Assuming that you have:This gives you: