【问题标题】:What is the best way to create a Json table using python?使用 python 创建 Json 表的最佳方法是什么?
【发布时间】:2019-06-26 11:48:09
【问题描述】:

我从 sql 数据库中提取数据,并希望将该数据存储为 json 表。目前我的输出为 csv,以便能够在 excel 中轻松地可视化数据。除了如何格式化和使用 json 文件,我了解一切。

【问题讨论】:

  • 什么是“json表”?
  • 基本上是一个 html 表,但我猜是使用 json?

标签: python json datatable


【解决方案1】:

将 CSV 转换为 JSON。

import csv
import json

csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')

fieldnames = ("FirstName", "LastName", "IDNumber", "Message")
reader = csv.DictReader(csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')

更可行的解决方案

import pandas as pd

df = pd.read_csv('final_coupa.csv')
df['json'] = df.apply(lambda x: x.to_json(), axis=1)

df['json'].to_csv('final_json', index=False)

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多