Python 如何显示可读的JSON列表?
我这里有一个来自Firebase的JSON字符串。我是通过Pythons Pyrebase获取的。 然而,有一个大问题 我得到的JSON是一个列表,例如一个清单:Python 如何显示可读的JSON列表?,python,json,firebase,Python,Json,Firebase,我这里有一个来自Firebase的JSON字符串。我是通过Pythons Pyrebase获取的。 然而,有一个大问题 我得到的JSON是一个列表,例如一个清单: u'Wardrode u'No u'Behind Table u'No u'dust-dree 这是一个Json字符串,但我替换了一些注释字符串: Using result1 = result1.replace("u'", "") result1 = result1.replace("{", "") r
u'Wardrode u'No u'Behind Table u'No u'dust-dree
这是一个Json字符串,但我替换了一些注释字符串:
Using
result1 = result1.replace("u'", "")
result1 = result1.replace("{", "")
result1 = result1.replace("}", "")
result1 = result1.replace("'", "")
result1 = result1.replace(":", "")
result1 = result1.replace("\u", "")
我想用一个换行符替换是和否,这样当我在HTML中显示它时,它就成为一个合适的列表
以下是我的完整代码:
result1 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-in/Inspector/', None)
for key in result1.keys():
key = sorted(result1.keys())
currentdate = key
print currentdate
for key in result1.keys():
inspector = key
print result1
我想使用从firebase获得的一行json列表生成一个像样且可读的输出
看起来是这样的:
{
"Rooms" : {
"Room1" : {
"2017-11-29" : {
"Inspection" : {
"Scan-in" : {
"Inspector" : {
"brix" : {
"Nov 29, 2017 11:20:15 PM" : {
"Checklist" : {
"1 Bible, Environment Card, Telephone Directory, Amenity Card, Room Attendant’s Card, Asmara Spa Tent Card, TV remote?" : "No",
"1 bath gel?" : "No",
"1 bath mat hanging on shower glass door handle?" : "No",
"1 bath soap" : "No",
"1 extra pillow with pillow cover?" : "No",
"1 facial tissue in a tissue box" : "No",
"1 lotion" : "No",
"1 pringles, 1 cashew nut, 1 cup noodles (placed in the coffee tray on the writing desk)?" : "No",
"1 sanitary bag" : "No",
"1 set of iron and board?" : "No",
"1 set of laundry list and bag?" : "No",
"1 shampoo?" : "No",
"1 shower cap" : "No",
"1 vanity kit" : "No",
"2 Tumbler overturned with coaster" : "No",
"2 bath towels - on the towel rack?" : "No",
"2 bathrobes?" : "No",
"2 coke, 2 sprite, 1 C2 lemon, 1 C2 apple, 1 pineapple juice, 1 orange juice, 1 mineral water, 2 San Mig light, 2 pale pilsen?" : "No",
"2 cups and saucers (for Mountain Wing" : "No",
"2 dental kit" : "No",
"2 face towels – on vanity counter (For suites, premier and VIPs)?" : "No",
"2 hand towels ?" : "No",
"2 mugs overturned with coaster (for Lake Wing)?" : "No",
"2 pairs of slippers?" : "No",
"6 Hangers?" : "No",
"Air conditioning temperature is in good working condition Set at 17o C, low speed" : "No",
"Appearance door surface in good condition" : "Yes",
"Appearance door surface- in good condition" : "No",
"Appearance of the bed – neat and sheet tightened?" : "No",
"Arrangement of the sofa and table in order" : "No",
"Bedhead free from dust" : "No",
"Bedsheet and pillow cases clean" : "No",
"Behind table - dust" : "No",
"Ceiling – no cobweb, water marks and crack lines" : "No",
"Check appearance of the glass – clean" : "No",
"Check the table surface – clean" : "No",
"Check underneath cushion" : "No",
"Coffee tray with 3 coffee sticks, 3 creamer, 3 white sugar, 3 brown sugar, 1 equal or sweetener, 3 lipton tea, 2 mineral water, 3 cocktail napkin ?" : "No",
"Compendium with 1 stationary pad, 1 envelope, channel guide?" : "No",
"Curtains are neat, presentable with hooks all intact and curtain rod" : "No",
"Door surface in good condition" : "No",
"Door surface – clean" : "No",
"Drawers – clean and dust-free" : "No",
"Dust bin with liner" : "No",
"Dustbin under the table is clean" : "No",
"Elsafe open or working?" : "No",
"Entrance louver clean and dust-free" : "No",
"Eye viewer and fire escape plan in order" : "No",
"Floor vacuumed and stain free" : "No",
"Free from rubbish under the bed" : "No",
"Fridge is cold and clean" : "No",
"Glass panel and door -clean" : "No",
"Guest Comment Card?" : "No",
"Hairdryer" : "No",
"Handbasin, vanity counter, vanity mirror and shaving mirror – clean" : "No",
"Happenings This Week" : "No",
"Key card holder – in working order" : "No",
"Lamp or light" : {
"diffuser clean and working" : "No"
},
"Let the door close by itself to test the door closure – in working order" : "No",
"Lights and switches are working" : {
"Remarks" : ""
},
"Lights in working order" : "No",
"Luggage bench fabric top is clean" : "No",
"Magazine" : "No",
"Minibar Voucher or Slip" : "No",
"Pencil?" : "No",
"Privacy Sign or Make Up Room Sign" : "No",
"Pull the curtains to test if they are working" : "No",
}
}
我想通过删除不必要的文本使其可读性和可理解性。我可以通过
result1 = str(result1)
result1 = result1.replace("u'", "")
result1 = result1.replace("{", "")
result1 = result1.replace("}", "")
result1 = result1.replace("'", "")
result1 = result1.replace(":", "")
result1 = result1.replace("\u", "")
这使得我在第一个图像中的输入变成:
但是,我无法做到:
result1 = result1.replace("No,", "No\n")
result1 = result1.replace("Yes,", "Yes\n")
我想这样做是为了整理清单。
当字符串找到“是”或“否”但仍显示其值时,该字符串必须位于换行符上
任何关于如何对这些数据进行排序的新想法也都是受欢迎的
我想要的示例数据:
扫描:brix 2017年11月29日下午11点15分检查表咖啡托盘,3支咖啡棒、3支奶油、3支白糖、3支红糖、1支同等或甜味剂、3支立顿茶、2杯矿泉水、3张鸡尾酒餐巾?没有
。。。等等
我该怎么做?请帮忙
这是我的全部代码
我不这么认为。。。从您给出的json和显示的代码来看,您似乎跳过了brix键。并且显示的图像不是控制台输出,因此您没有显示HTML生成
无论如何,字符串替换是错误的解决方案。您正在字典中查看Python的unicode字符串。如果分析了内容,则不应看到u或{}字符
例如,试试这样的东西
x = "'brix"
print("Scan in " + x)
dates = sorted(result1[x].keys())
for d in dates:
print(d)
for check, status in result1[x][d]['Checklist'].items():
print(check, status)
对该作业使用str.replace操作是错误的工具。您正在弄乱时间字符串,并将unicode转义(如\u2013,是一个破折号)转换为数字。您应该使用json.loads来解析此json数据。然后,您可以通过对数据进行迭代并使用适当的格式调用进行打印,以您想要的方式打印它。然而,若您需要这方面的具体帮助,您应该将JSON数据的示例作为文本发布,以便人们可以在您的数据上测试他们的代码。无法从图像文件加载文本。请参阅。这个问题主要是关于将代码作为图像发布,但它也适用于数据。不清楚您是如何从中生成HTML的,但如果您想要换行符,python中的换行符不会被转换为HTML段落。顺便说一句,您需要在sortedresult1.keys中输入key。。。不要在循环中排序并重置键变量
x = "'brix"
print("Scan in " + x)
dates = sorted(result1[x].keys())
for d in dates:
print(d)
for check, status in result1[x][d]['Checklist'].items():
print(check, status)