将整个文件加载到json中

时间:2019-03-14 16:53:27

标签: python python-3.x

我有一个看起来像这样的文本文件,

['Student_Name_1', 'Age_val', 'AddressLine', 'class d', '03-21-1968', 'connor', 'sample', '1 wonderful drive', 'director of public safety', 'signature']

我需要将此文本作为一组加载,并转换为如下所示的json,

{
    "key_1": "Student_Name_1",
    "key_2": "Age_val",
    "key_3": "AddressLine",
    "key_4": "class d",
    "key_5": "03-21-1968",
    "key_6": "connor",
    "key_7": "sample",
    "key_8": "1 wonderful drive",
    "key_9": "director of public safety",
    "key_10": "signature"
}

我将整个文件内容读取为字符串“值”,并尝试先通过添加键将其转换为set,然后转换为json。

这是我的代码,用于将字符串转换为set,但无法正常工作,

value = "['Student_Name_1', 'Age_val', 'AddressLine', 'class d', '03-21-1968', 'connor', 'sample', '1 wonderful drive', 'director of public safety', 'signature']"

val = set(value)
print(type(val))
for x in val:
    print(x)

0 个答案:

没有答案