Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

Python json - ' ' when i try to print or assign its value to a variable

i am reading .csr file in a variable which keeps its format as is, so when i print variable it prints just like its stored in a file. when i assign this to a key in dictionary i see ' ' visible in it.

how can i keep the format of a text variable intake while converting to json.

ex.

    import json


if __name__ == "__main__":
# python3 test.py --appenv = 'dev' --certtype = 'add' --certpath = '/etc/ssl/certs/mssql.cer' --uname = test --pass = test

    a =    '''-----BEGIN CERTIFICATE-----
    MIIHyDCCBbCgAwIBAgITRQAeiysRV+gPdQpj/gABAB6LKzANBgkqhkiG9w0BAQsF
    ADBbMRMwEQYKCZImiZPyLGQBGRYDbmV0MRgwFgYKCZImiZPyLGQBGRYIanBtY2hh
    c2UxFjAUBgoJkiaJk/IsZAEZFgZleGNoYWQxEjAQBgNVBAMTCVBTSU4wUDU1MTAe'''
    print(a)
    checkbody = {
        "action" : 'args.certtype',
        "fsid" : 'args.uname',
        "thumbprint" : 'thumbprint', # if renewing.
        "csr" :  a,  # read and pt csr here.
        "certType"  : "Internal Facing only for Mutual and/or Server Authentication",
        "noOfLicenses" : "1"
        }

    print(type(checkbody))

    print(json.dumps(checkbody))

When i run this it adding to it. i need to keep the format of a string. see output below.

    -----BEGIN CERTIFICATE-----
        MIIHyDCCBbCgAwIBAgITRQAeiysRV+gPdQpj/gABAB6LKzANBgkqhkiG9w0BAQsF
        ADBbMRMwEQYKCZImiZPyLGQBGRYDbmV0MRgwFgYKCZImiZPyLGQBGRYIanBtY2hh
        c2UxFjAUBgoJkiaJk/IsZAEZFgZleGNoYWQxEjAQBgNVBAMTCVBTSU4wUDU1MTAe 
<class 'dict'> 
{"action": "args.certtype", "fsid": "args.uname", "thumbprint": "thumbprint", "csr": "-----BEGIN CERTIFICATE-----
    MIIHyDCCBbCgAwIBAgITRQAeiysRV+gPdQpj/gABAB6LKzANBgkqhkiG9w0BAQsF
    ADBbMRMwEQYKCZImiZPyLGQBGRYDbmV0MRgwFgYKCZImiZPyLGQBGRYIanBtY2hh
    c2UxFjAUBgoJkiaJk/IsZAEZFgZleGNoYWQxEjAQBgNVBAMTCVBTSU4wUDU1MTAe", "certType": "Internal Facing only for Mutual and/or Server Authentication", "noOfLicenses": "1"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

755k questions

547k answers

4 comments

86.4k users

...