将字典列表打印为表格

时间:2019-04-30 22:16:05

标签: python-3.x string data-manipulation

如何使用Python将以下数据格式化为表格形式? 有什么方法可以按照预期的格式打印/写入数据?

[{"itemcode":null,"productname":"PKS543452","value_2018":null},
{"itemcode":null,"productname":"JHBG6%&9","value_2018":null},
{"itemcode":null,"productname":"VATER3456","value_2018":null},
{"itemcode":null,"productname":"ACDFER3434","value_2018":null}]

预期输出:

|itemcode | Productname | Value_2018 |
|null |PKS543452|null|
|null |JHBG6%&9|null|
|null |VATER3456|null|
|null |ACDFER3434|null|

2 个答案:

答案 0 :(得分:2)

您可以使用pandas从词典列表中生成数据框:

import pandas as pd

null = "null"
lst = [{"itemcode":null,"productname":"PKS543452","value_2018":null},
{"itemcode":null,"productname":"JHBG6%&9","value_2018":null},
{"itemcode":null,"productname":"VATER3456","value_2018":null},
{"itemcode":null,"productname":"ACDFER3434","value_2018":null}]

df = pd.DataFrame.from_dict(lst)
print(df)

输出:

  itemcode productname value_2018
0     null   PKS543452       null
1     null    JHBG6%&9       null
2     null   VATER3456       null
3     null  ACDFER3434       null

这使以后轻松操作表中的数据变得容易。否则,您可以使用内置的字符串方法打印所需的输出:

output=[]
col_names = '|' + ' | '.join(lst[0].keys()) + '|' 
print(col_names)

for dic in lst:
    row = '|' + ' | '.join(dic.values()) + '|'
    print(row)

输出:

|itemcode | productname | value_2018|
|null | PKS543452 | null|
|null | JHBG6%&9 | null|
|null | VATER3456 | null|
|null | ACDFER3434 | null|

答案 1 :(得分:1)

您也可以尝试这样(不使用熊猫)。我已经在代码本身中注释了每一行,所以请不要忘记阅读它们。

  

注意::实际上,您粘贴的列表/数组是json.dumps()的结果(在Python中为文本),或者您已经复制了API响应(JSON) 。

     

null来自JavaScript,并且粘贴的列表/数组不是有效的Python列表,但可以将其视为文本并使用json.loads()转换回Python列表。在这种情况下,null将转换为None

     

这就是为什么要形成所需的o / p的原因,我们需要另一支类似"null" if d[key] is None else d[key]的支票。

import json

# `null` is used in JavaScript (JSON is JavaScript), so I considered it as string
json_text = """[{"itemcode":null,"productname":"PKS543452","value_2018":null},
{"itemcode":null,"productname":"JHBG6%&9","value_2018":null},
{"itemcode":null,"productname":"VATER3456","value_2018":null},
{"itemcode":null,"productname":"ACDFER3434","value_2018":null}]"""

# Will contain the rows (text)
texts = []

# Converting to original list object, `null`(JavaScript) will transform to `None`(Python)
l = json.loads(json_text)

# Obtain keys (Note that dictionary is an unorederd data type)
# So it is imp to get keys for ordered iteration in all dictionaries of list
# Column may be in different position but related data will be perfect
# If you wish you can hard code the `keys`, here I am getting using `l[0].keys()`
keys = l[0].keys()

# Form header and add to `texts` list
header = '|' + ' | '.join(keys) + " |"
texts.append(header)

# Form body (rows) and append to `texts` list
rows = ['| ' + "|".join(["null" if d[key] is None else d[key] for key in keys]) + "|" for d in l]
texts.extend(rows)

# Print all rows (including header) separated by newline '\n'
answer = '\n'.join(texts)
print(answer)
  

输出

|itemcode | productname | value_2018 |
| null|PKS543452|null|
| null|JHBG6%&9|null|
| null|VATER3456|null|
| null|ACDFER3434|null|