【问题标题】:Python csv columns to JSONPython csv 列到 JSON
【发布时间】:2017-07-29 15:01:26
【问题描述】:

我有以下 csv

id, o1, o2, o3
'jess', 1.0, 4, 0.3
'jill', 0, 5, 0.123
'jamie', -3, 0.2, 1.0

并希望它在嵌套的 json 中,每列作为 json 键在标题名称上:

myjson = {
    "o1": {"jess": 1.0, "jill":   0, "jamie": -3}, 
    "o2": {"jess":   4, "jill":   5, "jamie": 0.2}, 
    "o3": {"jess": 0.3, "jill": 0.2, "jamie": 1.0}, 

不确定执行此操作的最佳(最 Pythonic)方法。这是我的第一次尝试:

import csv
with open(myfile, "r") as f:
    reader = csv.reader(f, delimiter=',', quotechar='"')
    first = True
    for line in reader:
        if first:
            myjson = {key: dict() for key in line}
            header = list(line)
            first = False
        for i in range(len(header)):
            id = line[0]
            myjson[header[i+1]][id] = line[i+1]

我认为有更好的方法来做到这一点。

编辑: 早该指定,但我不想使用 Pandas 之类的东西。这需要以最小的包依赖性超快。

【问题讨论】:

  • Please find the answer here, 希望对您有所帮助。
  • @Bhargav 抱歉,这不是答案。该答案是获取每一行并制作一个以列名为键的字典。我正在尝试获取每一列并在该行的第一个条目上创建一个 dict。

标签: python json csv


【解决方案1】:

这可能是“作弊”,但这总是对我有用。如果还没有 - 没有什么是小代码无法解决的。但我使用Pandas 模块。它确实满足了我的很多数据需求。我将 csv 读入 DataFrame,然后将 dataframe 放入 JSON(或任何其他格式)

import pandas as pd

df1 = pd.read_csv('YOUR_PATH_HERE')

df1.to_json('PATH_HERE')

它超级简单且易于定制。您可能需要摆弄更多变量。这是文档:read_csvto_json 这总是一本好书:10 Minutes to Pandas

【讨论】:

  • 对不起,我不应该指定熊猫。我试图不依赖额外的包,也不想花时间建立一个 DataFrame 只是为了放弃它。不过,这完全是最简单的方法!
  • @Sal,明白了。但是,对于您关于“构建数据框以放弃它”的观点 - 创建数据框需要一分钟。不要因为放弃你的数据宝贝而感到难过。
  • 设计规范实际上说“没有熊猫”。 :)
【解决方案2】:

我肯定认为下面的答案太长了,但如果你仍然需要答案,这行得通。 我根据您的数据创建了一个 test.csv

我不知道你为什么要使用 pandas 来消除,但无论如何

import csv
import itertools
from itertools import *
import json


def read_with_header():
    with open ('/Users/bhargavsaidama/test.csv', 'rb') as f:
        reader = csv.reader(f, delimiter = ',', quotechar = '|')
        row_count = 0
        keys = []
        for row in reader:
            row_count = row_count + 1
            keys.append(row)
        header = keys[0]
        return row_count, header


def reading_ignore_header():

    row_count, header = read_with_header()

    with open('/Users/bhargavsaidama/test.csv', 'rb') as f:
        f.next()
        # row_count = sum(1 for row in f)
        # # print row_count
        reader = csv.reader(f, delimiter = ',' , quotechar = '|')
        result = []
        values = ()

        for row in reader:
            # row_count is taken including header file in the above function
            values = tuple((itertools.combinations(row, 2)))[:(row_count-1)]  # row_count is important,since your keys are rows

            for x, y in values:
                result.append({x:y})
        return result, header

# The following function is taken from here
# http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]


def main():

    result, header = reading_ignore_header()
    final_values = list(chunks(result,3)) # here 3 reflects (row_count-1)
    header = header[1:]        # seems like u wanna ignore Id 
    data_str = json.dumps(dict(zip(header, final_values)))
    data_json = json.loads(data_str)
    print data_str, data_json
    return data_str, data_json


if __name__ == "__main__":
    main()

希望对您有所帮助,如果您可以对其进行优化,请继续执行。我也会学习的:)

谢谢

【讨论】:

    【解决方案3】:

    这是一个更简单的解决方案,您将需要 pyexcel 和 pyexcel-text:

    >>> import pyexcel as p
    >>> sheet=p.get_sheet(file_name='test.csv')
    >>> sheet
    test.csv:
    +---------+-----+-----+-------+
    | id      | o1  | o2  | o3    |
    +---------+-----+-----+-------+
    | 'jess'  | 1.0 | 4   | 0.3   |
    +---------+-----+-----+-------+
    | 'jill'  | 0   | 5   | 0.123 |
    +---------+-----+-----+-------+
    | 'jamie' | 3   | 0.2 | 1.0   |
    +---------+-----+-----+-------+
    >>> sheet.transpose()
    >>> sheet.name_columns_by_row(0)
    >>> sheet.name_rows_by_column(0)
    >>> sheet
    test.csv:
    +----+--------+--------+---------+
    |    | 'jess' | 'jill' | 'jamie' |
    +====+========+========+=========+
    | o1 | 1.0    | 0      | 3       |
    +----+--------+--------+---------+
    | o2 | 4      | 5      | 0.2     |
    +----+--------+--------+---------+
    | o3 | 0.3    | 0.123  | 1.0     |
    +----+--------+--------+---------+
    >>> sheet.get_json(write_title=False) # pip install pyexcel-text
    '{"o1": {"\'jamie\'": 3, "\'jess\'": 1.0, "\'jill\'": 0}, "o2": {"\'jamie\'": "0.2", "\'jess\'": 4, "\'jill\'": 5}, "o3": {"\'jamie\'": 1.0, "\'jess\'": "0.3", "\'jill\'": "0.123"}}'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 2016-04-19
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多