Get the App
SLTechnology News&Howtos  ›  Development  › 

How python encapsulates json format strings and handles the problem of single and double quotation marks

Shulou Source: shulou.com Published: 2022-06-01 15:11:31 09月28日 Update

This article mainly introduces how python encapsulates json format strings and deals with single and double quotation marks related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this python how to encapsulate json format strings and deal with single and double quotation marks article will have a harvest, let's take a look at it.

When using python to do web service, there will be data exchange. Most of the data will be exchanged in json format at present. After receiving the standard json format data, it will be more convenient to parse the data.

I have a scenario where the data of three array types is encapsulated into a string in json format and sent to the consumer. The data of the three array types are:

Print (boxes) print (scores) print (txts) # the following is the print result # first: [array ([[292.294.], [331.293.], [349.848.], [309.850.], dtype=float32), array ([339. 299.], [378.298.], [392.660.], [353.662.]] Dtype=float32)] # second: [0.9401206, 0.8182683] # third: ['Research Center for Land improvement and soil Restoration', 'East Map' of South China Agricultural University]

I encapsulate it in str format like this:

Txts_str = "" for i in range (len (txts)): if (len (txts_str) = = 0): txts_str = str (txts [I]) else: # because it is a string sentence Choose an uncommonly used symbol "|" to do the interval txts_str = txts_str + "|" + str (txts [I]) txt_scores = "" for i in range (len (scores)): if (len (txt_scores) = = 0): txt_scores = str (scores [I]) else: txt_scores = txt_scores + "|" + Str (for j in range [I]) txt_boxes = "" for i in range (len (boxes)): for j in range (len (boxes [I])): for k in range (len (boxes [I])): if (len (txt_boxes) = = 0): txt_boxes = str (boxes [I] [j]) [K]) else: txt_boxes = txt_boxes + " "+ str (Boxes [I] [j] [k]) # print print (txt_boxes) print (txts_str) print (txt_scores) # print result: # 292.0294.0331.0293.0349.0848.0309.0850.0339.0298.0298.0392.0660.0353.0662.Research Center for Land renovation and soil Restoration in South China Agricultural University

Encapsulated into json:

# then encapsulated into json:result_data = {"txt_boxes": txt_boxes, "txts": txts, "txt_scores": txt_scores}

Found to be in single quotation mark format, not standard json:

# {'txt_boxes':' 292.0294.0331.0293.0349.0848.0309.0850.0339.0299.0378.0298.0392.0660.0353.0662.0, 'txts': [' Research Center for Land improvement and soil Restoration', 'East Map of South China Agricultural University'], 'txt_scores':' 0.9401206 | 0.8182683'}

This changes single quotation marks to double quotation marks

Json.dumps (result_data)

It is found that the Chinese format is incorrect after json.dumps:

# {"txt_boxes": "292.0294.0331.0293.0349.048.0309.0850.0339.0299.0378.0298.0392.0660.0353.0662.0", "txts": "\ u571f\ u5730\ u6574\ u6cbb\ u4e0e\ u571f\ u4fee\ u590d\ u7814\ u7a76\ u4e2d\ u5fc3 |\ u534e\ u5357\ u519c\ u4e1a\ u5927\ u5b66\ u4e28\ u4e1c\ u56fe", "txt_scores": "0.9401206 | 0.8182683"}

This is because the default ascii encoding is used in json.dumps serialization. To output real Chinese, you need to specify ensure_ascii=False:

Json.dumps (result_data,ensure_ascii=False)

The result is the standard json format we want:

# {"txt_boxes": "292.0294.0331.0293.0349.0848.0309.0850.0339.0299.0378.0298.0392.0660.0353.0662.0", "txts": ["Research Center for Land improvement and soil Restoration | East Map of South China Agricultural University"] "txt_scores": "0.9401206 | 0.8182683"} this is the end of the article on "how python encapsulates json strings and handles single and double quotation marks" Thank you for reading! I believe you all have a certain understanding of "how python encapsulates json format strings and deals with single and double quotation marks". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Format encapsulation quotation marks characters strings data problems processing agriculture land soil university research center South China South China Agricultural University research three standards knowledge results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi macOS vpn NVidia