尝试在 blaze 中使用 json.dumps 时发生错误说 TypeError: object is not JSON serializable。
data = Data("employee.json")
json.dumps(data)
您不能直接将其转换为 JSON。
替代方式如下:
fields = [] # Create an empty list to hold the field names
for fieldName in data.fields: # Iterate the field names
fields.append(fieldName) # Add to the list
result = []
for row in data: # Iterate each row
currentRow = {}
count = 0
for value in row:
currentRow[fields[count]] = value # Add each value with corresponding key from fields
count = count + 1
result.append(currentRow)
print(json.dumps(result))
如果您想要 JSON 文件的交互式视图,只需评估其名称即可。你会得到相当于print data.__repr__()
from blaze import Data
from blaze.utils import example
data = Data(example('accounts.json'))
data
# amount name
# 0 100 Alice
# 1 -200 Bob
# 2 300 Charlie
# 3 400 Dennis
# 4 -500 Edith