Python 笔记本中的 !curl 命令失败,出现 500 内部错误
!curl commands in Python notebook fail with 500 Internal error
我 运行 Google Colab 中的以下代码并得到 The server encountered an internal error or misconfiguration and was unable to complete your request
。如果我 运行 命令没有像下面那样传入变量 $data
,它运行得很好。只有当我遍历文件并传递变量时,它似乎失败了
import csv
import json
reader = csv.reader(open('/content/drive/MyDrive/file5.csv'))
for row in reader:
data = {"snps": row[0], "pop": "YRI", "r2_threshold": "0.9", "maf_threshold": "0.01"}
data = json.dumps(data)
data = "'{}'".format(data)
!curl -k -H "Content-Type: application/json" -X POST -d "$data" 'https://ldlink.nci.nih.gov/LDlinkRest/snpclip?token=e3e559472899'
这个有效:
!curl -k -H "Content-Type: application/json" -X POST -d '{"snps": "rs3\nrs4", "pop":"YRI", "r2_threshold": "0.1", "maf_threshold": "0.01"}' 'https://ldlink.nci.nih.gov/LDlinkRest/snpclip?token=e3e559472899'
更新: 实际上,ipython
确实允许您 运行 !
在循环中转义;您代码中的实际错误纯粹是引用不正确(尤其是在 data
值周围添加了单引号,但可能还有更多)。
下面是原来的(部分不正确的)答案。
!
escape 告诉你的笔记本(Google Colab,Jupyter,或者你有什么;基本上任何 运行ning ipython
作为内核或类似的东西)留下 Python 和 运行 一个 shell 命令。 Python 本身不支持这个;最接近的近似值类似于
import subprocess
...
for row in reader:
data = {"snps": row[0], "pop": "YRI", "r2_threshold": "0.9", "maf_threshold": "0.01"}
data = json.dumps(data)
# This was wrong on so many levels
# data = "'{}'".format(data)
subprocess.run(['curl', '-k',
'-H', "Content-Type: application/json",
'-X', 'POST', '-d', data,
'https://ldlink.nci.nih.gov/LDlinkRest/snpclip?token=e3e559472899'],
text=True, check=True)
虽然避免使用 subprocess
和 运行 宁 Python urllib
or requests
代码来执行 POST
会更高效和优雅,并给你更多的控制权关于发送的内容及其处理方式。
如何在 shell 命令和 Python 之间转换时正确引用字符串需要您了解 shell 的引用行为。我只是简单地指出,我在您的原始命令中不正确的地方留下了双引号,但在其他方面更喜欢单引号,当然,data
现在指的是具有该名称的正确 Python 变量,而不是同名的 shell 变量。
重申一下:ipython
(这是你的笔记本的接口)知道如何 运行 Python 代码和 shell scipt 代码通过 !
;但是一旦你要求它 运行 Python 代码,ipython
将它交给 Python 适当的,你就不再在 ipython
.
我 运行 Google Colab 中的以下代码并得到 The server encountered an internal error or misconfiguration and was unable to complete your request
。如果我 运行 命令没有像下面那样传入变量 $data
,它运行得很好。只有当我遍历文件并传递变量时,它似乎失败了
import csv
import json
reader = csv.reader(open('/content/drive/MyDrive/file5.csv'))
for row in reader:
data = {"snps": row[0], "pop": "YRI", "r2_threshold": "0.9", "maf_threshold": "0.01"}
data = json.dumps(data)
data = "'{}'".format(data)
!curl -k -H "Content-Type: application/json" -X POST -d "$data" 'https://ldlink.nci.nih.gov/LDlinkRest/snpclip?token=e3e559472899'
这个有效:
!curl -k -H "Content-Type: application/json" -X POST -d '{"snps": "rs3\nrs4", "pop":"YRI", "r2_threshold": "0.1", "maf_threshold": "0.01"}' 'https://ldlink.nci.nih.gov/LDlinkRest/snpclip?token=e3e559472899'
更新: 实际上,ipython
确实允许您 运行 !
在循环中转义;您代码中的实际错误纯粹是引用不正确(尤其是在 data
值周围添加了单引号,但可能还有更多)。
下面是原来的(部分不正确的)答案。
!
escape 告诉你的笔记本(Google Colab,Jupyter,或者你有什么;基本上任何 运行ning ipython
作为内核或类似的东西)留下 Python 和 运行 一个 shell 命令。 Python 本身不支持这个;最接近的近似值类似于
import subprocess
...
for row in reader:
data = {"snps": row[0], "pop": "YRI", "r2_threshold": "0.9", "maf_threshold": "0.01"}
data = json.dumps(data)
# This was wrong on so many levels
# data = "'{}'".format(data)
subprocess.run(['curl', '-k',
'-H', "Content-Type: application/json",
'-X', 'POST', '-d', data,
'https://ldlink.nci.nih.gov/LDlinkRest/snpclip?token=e3e559472899'],
text=True, check=True)
虽然避免使用 subprocess
和 运行 宁 Python urllib
or requests
代码来执行 POST
会更高效和优雅,并给你更多的控制权关于发送的内容及其处理方式。
如何在 shell 命令和 Python 之间转换时正确引用字符串需要您了解 shell 的引用行为。我只是简单地指出,我在您的原始命令中不正确的地方留下了双引号,但在其他方面更喜欢单引号,当然,data
现在指的是具有该名称的正确 Python 变量,而不是同名的 shell 变量。
重申一下:ipython
(这是你的笔记本的接口)知道如何 运行 Python 代码和 shell scipt 代码通过 !
;但是一旦你要求它 运行 Python 代码,ipython
将它交给 Python 适当的,你就不再在 ipython
.