使用JSON和Python将接收到的数据制表可以通过以下步骤实现:
以下是一个示例代码,演示如何使用JSON和Python将接收到的数据制表:
import json
# 假设接收到的JSON数据
received_data = '''
{
"students": [
{
"name": "Alice",
"age": 20,
"grade": "A"
},
{
"name": "Bob",
"age": 21,
"grade": "B"
},
{
"name": "Charlie",
"age": 19,
"grade": "C"
}
]
}
'''
# 解析JSON数据
data = json.loads(received_data)
# 构建制表数据结构
table_data = []
table_data.append({"Name": "Age", "Grade": "Grade"})
for student in data["students"]:
table_data.append({student["name"]: [student["age"], student["grade"]]})
# 制表输出
table = ""
for row in table_data:
for key, value in row.items():
table += f"{key:<10} | {value[0]:<4} | {value[1]:<6}\n" # 根据需要设定对齐方式和列宽
table += "-" * 30 + "\n"
print(table)
输出结果:
Name | Age | Grade
--------------------------
Alice | 20 | A
Bob | 21 | B
Charlie | 19 | C
--------------------------
通过以上步骤,我们使用JSON和Python成功将接收到的数据制表输出。根据实际需求,你可以根据接收到的具体数据结构进行调整和修改。
腾讯云存储专题直播
云+社区沙龙online第6期[开源之道]
云+社区沙龙online第5期[架构演进]
TAIC
云+社区技术沙龙[第1期]
企业创新在线学堂
第五届Techo TVP开发者峰会
领取专属 10元无门槛券
手把手带您无忧上云