将字典键/值pai的单引号转换为双引号

2024-05-19 21:56:34 发布

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

我有一个单反逗号键值对的词汇表,如下所示:

filename = 'sub-310621_task-EMOTION_acq-LR_bold.nii.gz'
intended_for ={"IntendedFor", filename}

由于我想把这个字典写到一个json文件中,我必须在两个倒逗号之间使用filename,例如:"sub-310621_task-EMOTION_acq-LR_bold.nii.gz"

所以输出应该是:

intended_for ={"IntendedFor", "sub-310621_task-EMOTION_acq-LR_bold.nii.gz"}

此输出将写入test.json文件,该文件应如下所示:

{
    "IntendedFor": "sub-310621_task-EMOTION_acq-LR_bold.nii.gz"
}

我怎么能用python做这个?


Tags: 文件jsonfortaskfilename键值逗号gz
5条回答

要在java中创建线程,有two ways
1.通过实现Runnable接口
2.通过扩展Thread

如果实现Runnable接口,则需要将Runnable对象传递给Thread
构造函数

这样,您的对象将获得线程行为

如果扩展Thread类,则需要创建Thread扩展类的对象
这样,您的对象将获得线程行为

但你没有遵循上述两种方式中的任何一种

在代码中,语句newthread1 t11 = new newthread1();只创建简单对象
不是Thread object
但是您试图在normal object上调用Thread方法会导致编译错误

为了避免错误,您需要遵循以上两种方法中的任何一种
更具体地说,您必须
用此语句替换newthread1 t11 = new newthread1();

 Thread thread = new Thread(new newthread1());//first way implements Runnable

或用此语句替换class newthread1 implements Runnable{

class newthread1 extends Thread implements Runnable{//second way extends Thread

第一个错误,您已将引用变量定义为t11

 newthread1 t11=new newthread1();

因此,在代码中使用t11

boolean b=t11.t.isAlive(); // change t1 to t11

其次,在newthread1newthread2类中没有定义Thread t实例变量。这可能有助于:

class newthread1 implements Runnable{
  Thread t; // make this an instance variable , currently it is local to constructor
  newthread1()
 {
   t=new Thread(this,"FirstThread");
   System.out.println("Child Thread:"+t);
   t.start();
 }

解决方案1
将主要方法更改为

class mainthread {
    public static void main(final String args[]) throws InterruptedException {
        Thread thread = new Thread(new newthread1());
        newthread1 t11 = new newthread1();
        new newthread2();
        boolean b = thread.isAlive();
        System.out.println("Thread is alive:" + b);
        thread.join();
    }
}

要运行线程调用thread.start(),创建可运行对象的指令不会自动开始运行。您明确地告诉线程开始或停止

解决方案2
或者您可以将线程对象't'创建为global varibable,并将类更改为

class newthread1 implements Runnable {
    public Thread t;

    newthread1() {
        t = new Thread(this, "FirstThread");
        System.out.println("Child Thread:" + t);
        t.start();
    }

    @Override
    public void run() {
        System.out.println("We are in processing for 1st thread");
        int p = 1000, t = 3;
        double r = 3.5, si;
        try {
            si = p * r * t / 100;
            System.out.println("Simple Interest:" + si);
        } catch (ArithmeticException e) {
            System.out.println("Error:" + e);
        }
    }

    public Thread getT() {
        return t;
    }
}

然后主要方法如下:

class mainthread {
    public static void main(final String args[]) throws InterruptedException {
        newthread1 t11 = new newthread1();
        new newthread2();
        boolean b = t11.t.isAlive();
        System.out.println("Thread is alive:" + b);
        t11.t.join();
    }
}

我个人建议您首先学习Java基础知识的教程。我从以下几点确信您不清楚基本Java:

boolean b=t1.t.isAlive();

您没有一个名为t1的变量,但仍然尝试使用它

编译器找不到任何名为t1的变量,它会抱怨Cannot find symbol t1

我想你想用t11

此外,即使使用t11,它仍然会抱怨,因为在类newthread1中没有t作为类变量,而是在构造函数中定义了一个局部变量

还可以尝试阅读一些java标准,如如何声明类、命名约定等

这对你将来会有很大帮助

I'm trying to use isALive

没有这样的方法。你是说isAlive()

and join method but its throwing an error like can not find symbol

这是一个编译错误,可能是因为拼写错误。编译错误被打印,而不是“抛出”

please tell me where is the error exactly in this program.

编译器已经完成了这项工作,而您尚未将其发布到此处。如果您希望其他人为您重新编译程序,只是为了找到错误站点,我建议您可能需要等待很长时间

and what is the use of join method.i know that it is a wait for threads to finish but i want in details.

详情见Javadoc。我可以在这里引用它,但坦率地说,我看不出你应该已经读过它的意义。如果你有什么不明白的地方,你应该在问题中这样说

相关问题 更多 >