Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 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"}
question from:https://stackoverflow.com/questions/65834171/python-json-n-when-i-try-to-print-or-assign-its-value-to-a-variable

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...