如何在python中将json转换为纯文本

时间:2018-08-15 14:53:04

标签: python json

我已经编写了一个python函数,它将通过邮递员进行api调用

我也在这样的行中以json格式获取结果

  

[{u'major':1,u'uuid':u'b5b182c7-eab1-4988-aa99-b5c1517008d8',   u'imageurl':u'',u'name':u'资产0',u'location':{u'lampid':   u'40000c2a691226b3',u'经度':-121.91999816894531,u'altitude':   33.269363438,u'lampname':u'LAMP_3',u'floorid':u'50522CB5-F8F8-4F43-808A-44CFD86EC0CF',u'suiteid':   u'2FF35822-81DB-4469-8AE3-95D34B6EEDB1',u'lastseen':   u'2018-08-15T11:10:06.510Z',u'latitude':37.40650177001953,u'rssi':   -39,u'buildingid':u'93CB2950-46D0-4231-B21C-C4691ACFCC1D'},u'assettypeid':u'07810021-DD92-4BA9-946B-4829AD8E56DC',u'id':   u'226854A8-C634-4BB8-9959-0A080B932BD0',u'未成年人':5,u'description':   None},{u'locationError':u'找不到位置',u'major':1,u'uuid':   u'b5b182c7-eab1-4988-aa99-b5c1517008d8',u'imageurl':u'',u'name':   u'资产3',u'assettypeid':u'07810021-DD92-4BA9-946B-4829AD8E56DC',   u'id':u'E6DBA4C2-C2E9-4162-83F4-5906101F8EE8',u'minor':8   u'description':无},{u'major':1,u'uuid':   u'b5b182c7-eab1-4988-aa99-b5c1517008d8',u'imageurl':u'',u'name':   u'资产2',u'位置':{u'lampid':u'40000c2a691226b3',   u'longitude':-121.91999816894531,u'altitude':33.269363438,   u'lampname':u'LAMP_3',u'floorid':   u'50522CB5-F8F8-4F43-808A-44CFD86EC0CF',u'suiteid':   u'2FF35822-81DB-4469-8AE3-95D34B6EEDB1',u'lastseen':   u'2018-08-15T11:10:12.552Z',u'latitude':37.40650177001953,u'rssi':   -48,u'buildingid':u'93CB2950-46D0-4231-B21C-C4691ACFCC1D'},u'assettypeid':u'07810021-DD92-4BA9-946B-4829AD8E56DC',u'id':   u'8EF8189A-C902-4BCA-B1CA-787FE8E137AA',u'未成年人':7,u'description':   None},{u'locationError':找不到位置',u'major':1,u'uuid

我要在块中

我的代码是

def getAllAssets(api, token):

    url = api + '/v1/assettracking/assets?inline=location'
    headers = {'authorization': 'Bearer ' + token, 'content-type': 'application/json'}

    data = requests.get(url,  headers=headers)
    if data.status_code == requests.codes.ok:
        binary = data.content
        All_Assets = json.loads(binary)
        #print("All_Assets = {0}".format(All_Assets))
        print(All_Assets)

3 个答案:

答案 0 :(得分:2)

import json
print json.dumps(All_Assets, sort_keys=True,
    indent=4, separators=(',', ': '))

来自文档https://docs.python.org/2/library/json.html

答案 1 :(得分:1)

为什么不使用 pprint 库打印输出。我想它将为您提供所需的输出

Private Sub cmbls_DropButtonClick()
Dim i As Long, LastRow As Long
Dim w As Workbook
 Set w = Workbooks.Open("C:\Users\Desktop\Inputs for Gate 1.xlsx")
 Set ssheet = w.Worksheets("Sheet1")
 'showing error in the below line LastRow'
 LastRow = Sheets(“Sheet1”).Range(“A” & Rows.Count).End(xlUp).Row  
 If Me.cmbls.ListCount = 0 Then
 For i = 2 To LastRow
 Me.cmbls.AddItem Sheets(“Sheet1”).Cells(i, “A”).Value
 Next i
 End If
End Sub

Private Sub cmbls_Change()
Dim i As Long, LastRow As Long
Dim w As Workbook
 Set w = Workbooks.Open("C:\Users\Inputs for Gate 1.xlsx")
 Set ssheet = w.Worksheets("Sheet1")
 LastRow = Sheets(“Sheet1”).Range(“A” & Rows.Count).End(xlUp).Row
 For i = 2 To LastRow
 If Sheets(“Sheet1”).Cells(i, “A”).Value = (Me.cmbls) Or _
 Sheets(“Sheet1”).Cells(i, “A”).Value = Val(Me.cmbls) Then
 Me.TextBox1 = Sheets(“Sheet1”).Cells(i, “B”).Value
 End If
 Next
End Sub

答案 2 :(得分:1)

filename = "my_json_file.json"
with open(filename, 'r') as fr:
    pre_ = fr.read()
    lines = pre_.split('\n')
    new_filename = filename.split('.')[0]+".txt" # To keep the same name except ext
    with open(new_filename, "a") as fw:
        fw.write("\n".join(lines))