我需要用这种结构创建一个JSON文件
[{"image_id": 0873, "caption": "clock tower with a clock on top of it"}, {"image_id": 1083, "caption": "two zebras are standing in the grass in the grass"} , .....
从包含
image_id 0873 caption clock tower with a clock on top of it
image_id 1083 caption two zebras are standing in the grass in the grass
image_id 1270 caption baseball player is swinging a bat at the ball
image_id 1436 caption man is sitting on the bed with laptop
我怎样才能开始这样做
尝试使用regexp-easy导入更复杂的模式。以下是@Kozubi answer的扩展版本:
假设每条线看起来像: 图像{image{u id}标题{caption} 您可以使用str方法
split(maxsplit=number)
将行拆分为四个部分对于迭代文件的所有行:
要将变量保存到JSON文件中,可以使用JSON模块:
这应该是诀窍:
相关问题 更多 >
编程相关推荐