程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How Python encapsulates JSON format strings and handles single and double quotation marks

編輯:Python

python How to package json Format strings and handle single and double quotation marks

This article mainly introduces python How to package json Format strings and deal with single and double quotation marks , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article python How to package json Format strings and deal with single and double quotation marks. Articles will gain something , Let's have a look .

In the use of python do web There will be data interaction during service , Most currently use json Format data to interact , The recipient receives a standard json After formatting data , It is more convenient to parse data .

I have a scenario here that encapsulates the data of three array types into json Format and send it to the consumer , The three array types of data are :

print(boxes)
print(scores)
print(txts)
# Here 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)]
# the second :
[0.9401206, 0.8182683]
# Third :
[' Land remediation and soil remediation research center ', ' South China Agricultural University Dongtu ']

I put it this way str Format :

txts_str = ""
for i in range(len(txts)):
if (len(txts_str) == 0):
txts_str = str(txts[i])
else:
        # Because it's a string sentence , Choose an unusual symbol “|” Make intervals
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(scores[i])
 
txt_boxes = ""
for i in range(len(boxes)):
for j in range(len(boxes[i])):
for k in range(len(boxes[i][j])):
if (len(txt_boxes) == 0):
txt_boxes = str(boxes[i][j][k])
else:
txt_boxes = txt_boxes + "," + str(boxes[i][j][k])
 
# A print
print(txt_boxes)
print(txts_str)
print(txt_scores)
 
# Print the results :
#292.0,294.0,331.0,293.0,349.0,848.0,309.0,850.0,339.0,299.0,378.0,298.0,392.0,660.0,353.0,662.0
# Land remediation and soil remediation research center | South China Agricultural University Dongtu
#0.9401206|0.8182683

Encapsulated into json:

 # And then it's packaged as json:
result_data = {"txt_boxes": txt_boxes, "txts": txts, "txt_scores": txt_scores}

It is found that the format is single quotation mark , It's not a standard json:

#{'txt_boxes': '292.0,294.0,331.0,293.0,349.0,848.0,309.0,850.0,339.0,299.0,378.0,298.0,392.0,660.0,353.0,662.0', 'txts': [' Land remediation and soil remediation research center ', ' South China Agricultural University Dongtu '], 'txt_scor
es': '0.9401206|0.8182683'}

This turns single quotation marks into double quotation marks

json.dumps(result_data)

Find out json.dumps The Chinese format is wrong :

#{"txt_boxes": "292.0,294.0,331.0,293.0,349.0,848.0,309.0,850.0,339.0,299.0,378.0,298.0,392.0,660.0,353.0,662.0", "txts": "\u571f\u5730\u6574\u6cbb\u4e0e\u571f\u58e4\u4fee\u590d\u7814\u7a76\u4e2d\u5fc3|\u534e\u5357\u519c\u4e1a\u5927\u5b66\u4e28\u4e1c\u56fe", "txt_scores": "0.9401206|0.8182683"}

This is because json.dumps Used by default when serializing ascii code , To output real Chinese, you need to specify ensure_ascii=False:

json.dumps(result_data,ensure_ascii=False)

The result is the standard we want json Format :

#{"txt_boxes": "292.0,294.0,331.0,293.0,349.0,848.0,309.0,850.0,339.0,299.0,378.0,298.0,392.0,660.0,353.0,662.0", "txts": [" Land remediation and soil remediation research center | South China Agricultural University Dongtu "], "txt_scores": "0.9401206|0.8182683"}

About “python How to package json Format strings and handle single and double quotation marks ” That's all for this article , Thank you for reading ! I'm sure you're right “python How to package json Format strings and handle single and double quotation marks ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved