python - 在python中将表数据框隐藏到json api请求
问题描述
我使用 pandas 在网站上抓取表格并获取数据框。我只需要来自具有长标题的数据框中的一列数据。我需要将这些标题插入到如下所示的 json api 请求中:
payload = "{\n \"campaign_id\": 1,\n \"identifiers\": [\n {\n \"identifier\": \"scraped titles\",\n \"type\": \"keyword\"\n }\n ]\n}"
正如你所看到的,title 是一个嵌套的字典对。(我认为这就是你所说的)。因此,我需要像上面那样迭代每个标题:{"identifier":"Scraped Title", "type":"keyword"}
对于整个表。
所以我拥有的是数据表:标识符 1 2 3 4 n
我需要的是
{'identifier': '1', 'type': 'keyword'}, {'identifier': '2', 'type': 'keyword'}, {'identifier': '3', 'type': '关键字'},{'标识符':'4','类型':'关键字'},{'标识符':'n','类型':'关键字'}
谢谢!
解决方案
尝试这个,
items = [1, 2, 3, 4, 5...]
# list comprehension..
print([{'identifier': item, 'type': 'keyword'} for item in items])
输出
[{'identifier': 1, 'type': 'keyword'}, {'identifier': 2, 'type': 'keyword'}..]
推荐阅读
- python - 创建模块,然后导入该模块
- c# - 如何使这个输出文件从数据网格视图到.xls文件的读写模式?目前它产生只读文件
- git - 我可以使用 pip 从私有 VSTS 存储库安装包吗?
- php - 如何使用 IFTTT 向公共 PHP url 发出 Web 请求
- android - 执行 getprop ro.build.version.release 等大多数命令时,ADB 不断崩溃
- javascript - 单击重新加载图标或单击 F5?重定向到另一个页面
- c - 指针运算出错
- java - RecyclerView 中的数据没有从 Fragment 传递到 Adapter
- python - 如何在不下载的情况下从 irawler 获取找到的 url 的数量
- java - 如何在 Java 9+ 中使用 JFileChooser 显示网络共享?