首页 > 解决方案 > 无法在 python 中格式化 .json 文件

问题描述

我有一个 python 代码(main.py),其中包含一个名为 json 的 python 模块,我想用它以一种舒适的方式存储一些重要的 key=value 对,以后可以使用。问题在于格式(结构)

为了您更好地理解,我在此处附上了一些代码

main.py文件包含主要代码。output.json文件显示了主文件的输出。expected_output.json文件包含格式(结构),它显示了我想如何存储我的数据(键=值)。

如果有人有兴趣分叉这个存储库,他/她受到了极大的邀请。

代码: main.py


############*****Importing Modules*****############

import random as _random # for making random passwords.
import sys as _sys # for taking command line arguments.
import getopt as _getopt # for making command line flags.
import json as _json


############*****String Decleration*****############

_sm_Letter = ['abcdefghijklmnopqrstuvwxyz'] # small letters to use as a string on the user's password.
_cap_Letter = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ'] # capital letters to use as a string on the user's password.
_sp_char = ['!?,:;()-/@_`~#$%=^+&{*}[]|<>\.'] # special charecters to use as a string on the user's password.
_Numbers = ['0','1','2','3','4','5','6','7','8','9'] # numbers to use as a string on the user's password.
_str = _sm_Letter + _cap_Letter + _Numbers + _sp_char # concatinating those lists
_str = ''.join(_str)
_str = _str*100


############*****Defining Command line Options*****############

_args = _sys.argv[1:] # Remove 1st argument (file name) from the list of command line arguments. 
_s_opt = "ahvl:um:" # short options
_l_opt = ["accounts","help","masterkey=","lenght=","version","update"] # long options


############*****Defining Some Imyportant stuffs*****############

def _usage(): #defining usage
    print ("Usage: "+ _sys.argv[0] + " -m [MASTERKEY]\n")
    print ("Example: "+ _sys.argv[0] + " -m your_master_key\n")
def _help(): #defining help section
    _usage()
    print ("A tool for generate random and strong encrypted password and to keep that password also.\n\nMandatory arguments to long options are mandatory for short options too.")
    print ("-a, --accounts          to know how many accounts are exists.")
    print ("-h, --help          you are now seeing this.")
    print ("-l, --lenght            to pass the lenght of your password.")
    print ("-v, --version           to see the version of this tool.")


############*****Creating Masterkey Genaration Algorithum*****############

def _create_Master_Key():
    _masterKey = _random.sample(_str, 1000)
    _masterKey = ''.join(_masterKey)
    print('\nYour password is : '+_masterKey)



############*****Trying To Get Info From The User From The Command Line Arguments*****############

try:
    # Parsing argument
    arguments, values = _getopt.getopt(_args, _s_opt, _l_opt)
    # checking each argument
    for currentArgument, currentValue in arguments:
        if  currentArgument in ("-a", "--accounts"):
            pass
            exit()
        elif currentArgument in ("-h", "--help"):
            _help()
            exit()
        elif currentArgument in ("-v", "--version"):
            print('Version: V1.0.0')
            exit()
        elif currentArgument in ("-l", "--lenght"):
            _pass = _random.sample(_str, int(currentValue))
            _pass = ''.join(_pass)
            print('\nYour password is : '+_pass)
            exit()
        elif currentArgument in ("-m", "--masterkey"):
            print ("Displaying file_name:", _sys.argv[0])
        #elif currentArgument in ("-v", "--version"):
        #   print (("Enabling special output mode (% s)") % (currentValue))
except _getopt.error as err:        
    # output error, and return with an error code       
    print (str(err))
    print ("Enter -h, --help for help.")
    exit()




############*****Taking Info From The User Manually and generating random password*****############

_info = {}
try:
    _len_string = int(input('Enter the lenght of your password :> ')) # for getting the lenght of the user's password.
except:
    print('Please enter valid info')
    exit()

def _main():
    with open('output.json', 'r') as _f:
        data = _json.load(_f)

    _pass = _random.sample(_str,_len_string)
    _pass = ''.join(_pass)
    print('\nYour password is : '+_pass)
    _ans = input('\nDo you want to keep this password[Y/N] :> ')
    if  _ans == 'Y' or _ans == 'y' :
        _user = input('\nEnter User Name :> ')
        _acc_Name = input('\nEnter Account Name :> ')
        _email = input('\nEnter Email :> ')
        _info[_acc_Name] = {
            'name': _user,
            'email': _email,
            'pass': _pass
        }
        data[_acc_Name].append(_info)
        s = _json.dumps(_info, indent=2)
        with open('output.json', 'w') as f:
            f.write(s)      
            exit()
    elif _ans == 'N' or _ans == 'n' :
        _main()
        exit()
    else:
        print('Please enter valid info')
        exit()

if __name__ == '__main__':
    _main()
    _create_Master_Key()

输出.json

{
  "facebook": {
    "name": "username",
    "email": "example@email.com",
    "pass": "password"
  }
}{
  "facebook": {
    "name": "username",
    "email": "example@email.com",
    "pass": "password"
  }
}{
  "gmail": {
    "name": "username",
    "email": "example@email.com",
    "pass": "password"
  }
}{
  "gmail": {
    "name": "username",
    "email": "example@email.com",
    "pass": "password"
  }
}

预期输出.json

{
    "facebook": [
        {
            "User": "username",
            "email": "example@email.com",
            "pass": "random_generated_password",
            "created on": "date"
        },
        {
            "User": "username",
            "email": "example@email.com",
            "pass": "random_generated_password",
            "created on": "date"
        }
    ],

    "gmail": [
        {
            "User": "username",
            "email": "example@email.com",
            "pass": "random_generated_password",
            "created on": "date"
        },
        {
            "User": "username",
            "email": "example@email.com",
            "pass": "random_generated_password",
            "created on": "date"
        }
    ]
}

任何人请解决这个错误

标签: pythonjsonpython-3.xlistdictionary

解决方案


您不能像 output.json 的样子那样将一个对象附加到一个对象上。

我将其更改data[_acc_Name].append(_info)为以下内容:

if len(data[_acc_Name]) == 0:
    print('no user for this account')
    data[_acc_Name].append([_info])
else:
    print('users exist for this account')
    data[_acc_Name].append(_info[_acc_Name])

得到这个输出

{
  "facebook": [
    {
      "name": "yul",
      "email": "noooo@no.com",
      "pass": "z8k7"
    },
    {
      "name": "abc",
      "email": "yyy@gmail.com",
      "pass": "4~R6|)$}"
    }
  ]
}

您可能需要添加一些代码来支持新帐户


推荐阅读