用JSON-python编写值

2024-04-19 19:29:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在将列表写入JSON文件时遇到问题,下面是代码:

import numpy as np
import json

filename = "sub-01_task-rest_bold.json"
with open(filename, 'r') as f:
    niiMeta = json.load(f)

rTime = niiMeta.get("RepetitionTime")
nSlices = niiMeta.get("dcmmeta_shape")
imType = niiMeta.get("ImageType")


def numberofslices(n):
    if "EPI" in imType and len(nSlices) == 4:
        h = n[2]
        return h
    else:
        return 0


def checktr(t):
    if t >= 1000:
        k = t*0.001
        return k
    else:
        return t


def slicetime(tr, n):

    tr = checktr(tr)
    n = numberofslices(n)
    lastslice = tr - tr/n
    esti = np.linspace(0, lastslice, n)

    return esti


addstc = slicetime(rTime, nSlices).tolist()
niiMeta["SliceTiming "] = addstc


with open(filename, 'w') as f:
    json.dump(niiMeta, f, indent=4)

它可以使用列表创建新密钥,但与原始文件不同: 原件:with horizontal line

输出:[带垂直线][2]

如您所见,它在列表中有一个选项卡。请帮我把它取下来。你知道吗


Tags: 文件importjson列表getreturndefas