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

时间:2022-09-15 13:41:42

I have a dictionory with key value pair in single inverted commas as follows:

我在单引号中有一个带键值对的词典,如下所示:

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

AS i am want to write this dictionary to a json file i have to have filename in between two inverted commas eg: "sub-310621_task-EMOTION_acq-LR_bold.nii.gz"

我想把这个字典写成一个json文件,我必须在两个引号之间有文件名,例如:“sub-310621_task-EMOTION_acq-LR_bold.nii.gz”

SO the output should look like:

所以输出应该是这样的:

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

This output will be written in to test.json file which should look like:

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

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

How can i do this in python ?

我怎么能在python中做到这一点?

2 个解决方案

#1


3  

Rather than trying to build the JSON string yourself you should use the json module to do the encoding.

您应该使用json模块进行编码,而不是尝试自己构建JSON字符串。

The json.dumps() method takes an object such as a dictionary with key value pairs and converts it into a JSON compliant string which can then be written to a file.

json.dumps()方法接受一个对象,例如带有键值对的字典,并将其转换为符合JSON的字符串,然后可以将其写入文件。

Instead of a dictionary, you created a set by using a comma , instead of a colon :

您使用逗号而不是冒号创建了一个集合,而不是字典:

intended_for = {"IntendedFor", filename}

The correct code for your input would be

输入的正确代码是

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

Then you can encode

然后你可以编码

import json
json_string = json.dumps(intended_for)

#2


4  

The apostrophes or quotation marks on the ends of a string literal are not included in the string - 'asdf' and "asdf" represent identical strings. Neither the key nor the value in your dict actually include the character ' or ", so you don't need to perform a conversion.

字符串文字末尾的撇号或引号不包含在字符串中 - 'asdf'和“asdf”表示相同的字符串。你的dict中的键和值都不包含字符'或',因此你不需要执行转换。

When you dump the dict to JSON, your JSON dumper will automatically surround strings with " characters, among other necessary escaping. For example, if you're using the json module, you can just do

当您将dict转储到JSON时,您的JSON转储器将自动包含带有“字符的字符串,以及其他必要的转义。例如,如果您使用的是json模块,则可以执行此操作

json_string = json.dumps(intended_for)

to produce a correctly-formatted JSON string. (If this does not work, you have some other bug you're not showing us.)

生成格式正确的JSON字符串。 (如果这不起作用,你还有其他一些你没有向我们展示的错误。)

#1


3  

Rather than trying to build the JSON string yourself you should use the json module to do the encoding.

您应该使用json模块进行编码,而不是尝试自己构建JSON字符串。

The json.dumps() method takes an object such as a dictionary with key value pairs and converts it into a JSON compliant string which can then be written to a file.

json.dumps()方法接受一个对象,例如带有键值对的字典,并将其转换为符合JSON的字符串,然后可以将其写入文件。

Instead of a dictionary, you created a set by using a comma , instead of a colon :

您使用逗号而不是冒号创建了一个集合,而不是字典:

intended_for = {"IntendedFor", filename}

The correct code for your input would be

输入的正确代码是

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

Then you can encode

然后你可以编码

import json
json_string = json.dumps(intended_for)

#2


4  

The apostrophes or quotation marks on the ends of a string literal are not included in the string - 'asdf' and "asdf" represent identical strings. Neither the key nor the value in your dict actually include the character ' or ", so you don't need to perform a conversion.

字符串文字末尾的撇号或引号不包含在字符串中 - 'asdf'和“asdf”表示相同的字符串。你的dict中的键和值都不包含字符'或',因此你不需要执行转换。

When you dump the dict to JSON, your JSON dumper will automatically surround strings with " characters, among other necessary escaping. For example, if you're using the json module, you can just do

当您将dict转储到JSON时,您的JSON转储器将自动包含带有“字符的字符串,以及其他必要的转义。例如,如果您使用的是json模块,则可以执行此操作

json_string = json.dumps(intended_for)

to produce a correctly-formatted JSON string. (If this does not work, you have some other bug you're not showing us.)

生成格式正确的JSON字符串。 (如果这不起作用,你还有其他一些你没有向我们展示的错误。)