python - 在python中开始没有按预期返回
问题描述
我有一个名为的 json 文件response
:
{
"id": "163698b3-1e6b-4087-b398-16e2f5b1e7ef",
"status": "Submitted"
},
{
"id": "ed433b9e-ea4e-4652-907e-03114f869774",
"status": "Submitted"
},
{
"id": "3cd662fb-afdd-47e1-abde-364d727cfdab",
"status": "Submitted"
}
我搜索以 开头的行status
:
search = '"status"'
with open('path/to/file') as response:
for line in response:
if line.startswith(search):
print(line)
而且我没有输出,这是为什么呢?我的最终目标是删除这条线。谢谢。
解决方案
代码:</p>
search = '"status"'
with open("test.json", "r") as f:
lines = f.readlines()
with open('test.json', "w") as response:
for line in lines:
if not line.lstrip().startswith(search):
print(line)
response.write(line)
结果:
{
"id": "163698b3-1e6b-4087-b398-16e2f5b1e7ef",
},
{
"id": "ed433b9e-ea4e-4652-907e-03114f869774",
},
{
"id": "3cd662fb-afdd-47e1-abde-364d727cfdab",
}
推荐阅读
- javascript - 如何在 Create React App 的 jest config 中设置时区?
- verilog - iverilog 错误:赋值语句左值中的语法
- python-3.x - 如何从 Python 中创建的按钮类中关闭 kivy 弹出窗口
- python - 如何在kivy的屏幕之间发送输入?
- reactjs - 如何在 Next-i18next / React i18next 中插入一个 Link 组件来改变文本中的位置
- python - python azure sdk - 克隆服务器
- android - 项目选项卡卡在加载屏幕上 如何查看左侧的项目文件夹?
- laravel - laravel 邮件门面队列功能将邮件发送到错误的邮件主机
- eclipse-emf - 设置 EMF EObject 的父级
- macos - 带有 AudioFileID 和加密文件的音频单元文件阅读器