简体   繁体   English

如何创建基于 CSV 的没有标题的 json 文件

[英]How to create a json file based on CSV with no headers

I have a csv file like this:我有一个像这样的 csv 文件:

'3', '8948', 'f678'
'3', '5654', 'f644'
'6', '5567', 'g3335'
'9', '4467', 'g3356'
'9', '7666', 'h4433'

The CSV holds various records. CSV 拥有各种记录。 The first column represents an ID field.第一列代表一个 ID 字段。

I have looped through the CSV file and added the rows to a list.我已经遍历 CSV 文件并将行添加到列表中。

I have then used that list to make a JSON file.然后,我使用该列表制作了 JSON 文件。 Which looks like this:看起来像这样:

[
    [
        "3",
        "8948",
        "f678"
    ],
    [
        "3",
        "5654",
        "f644"
    ],
    [
        "6",
        "5567",
        "g3335"
    ]
     ...

But as I understand it, I wont be able to read from this JSON and perform tasks on it?但据我了解,我将无法从此 JSON 读取并对其执行任务? From what I can see I need it to be a dictionary, but how can I make a dictionary from my CSV, especially since the ID field is repeated and wont be unique.从我所见,我需要它作为字典,但是如何从我的 CSV 制作字典,特别是因为 ID 字段是重复的并且不会是唯一的。 The only other option is to just use a row number, if this is correct - how do I create a dictionary from my CSV with a row number?唯一的其他选择是只使用一个行号,如果这是正确的 - 我如何从我的 CSV 创建一个带有行号的字典?

No third party libraries solution:没有第三方库解决方案:

import json
from csv import DictReader

with open("tmp/1.csv", "r") as f_in, open("tmp/result.json", "w") as f_out:
    dict_reader = DictReader(f_in, fieldnames=["field1", "field2", "field3"])
    json.dump(list(dict_reader), f_out)

However, if this isn't going to be the only transform to be applied to this table data, there's Table helper in convtools library ( docs | github ).但是,如果这不是要应用于此表数据的唯一转换,则convtools库中有Table helper ( docs | github )。

import json
from convtools.contrib.tables import Table

results = list(
    Table.from_csv(
        "tmp/1.csv", header=["field1", "field2", "field3"]
    ).into_iter_rows(dict)
)

with open("tmp/result.json", "w") as f:
    json.dump(results, f)

I'm guessing your code so far looks something like this:我猜你的代码到目前为止看起来像这样:

import csv
import json

data: list[list[str]] = []
with open("input.csv", newline="") as f_in:
    reader = csv.reader(f_in)
    for row in reader:
        data.append(row)

with open("data.json", "w") as f_out:
    json.dump(data, f_out, indent=2)

To address your first issue/concern about how valid this JSON is or isn't...为了解决您关于此 JSON 是否有效的第一个问题/疑虑...

The bigger concept to take away is that Python's json module produces valid JSON.更大的概念是 Python 的 json 模块产生有效的 JSON。 If the module didn't complain about something while you were dumping the data then the JSON is good.如果在您转储数据时模块没有抱怨什么,那么 JSON 就很好。

But to more directly address your concern, JSON can look like a lot of different things:但为了更直接地解决您的问题,JSON 可能看起来像很多不同的东西:

print(json.dumps(1))
print(json.dumps("A"))
print(json.dumps({}))
print(json.dumps([]))

Each one of those dumps() produces valid JSON.这些转储()中的每一个都会产生有效的 JSON。 I don't know how to formally prove those are valid, but I do trust tools like Python's json module (it's been vetted over many years of real-world use and probably the world over).我不知道如何正式证明这些是有效的,但我确实相信Python 的 json 模块之类的工具(它已经过多年实际使用的审查,可能在全世界都经过了审查)。 I also went to https://jsonlint.com/ and entered those simple examples directly and got "Valid JSON" for all.我还去了https://jsonlint.com/并直接输入了这些简单的示例并为所有人获得了“有效 JSON”。

Now, what to do about the JSON you have?现在,如何处理您拥有的 JSON?

You can process it the way it is, or you can create the structure you want by providing column names/keys yourself (assuming you know what the data represents):您可以按原样处理它,也可以通过自己提供列名/键来创建所需的结构(假设您知道数据代表什么):

data_keyed: list[dict[str, Any]] = []
with open("input.csv", newline="") as f_in:
    reader = csv.reader(f_in)
    for row in reader:
        data_row = {"Col1": row[0], "Col2": row[1], "Col3": row[2]}
        data_keyed.append(data_row)

with open("data_keyed.json", "w") as f_out:
    json.dump(data_keyed, f_out, indent=2)

and now we get:现在我们得到:

[
  {
    "Col1": "3",
    "Col2": "8948",
    "Col3": "f678"
  },
  {
    "Col1": "3",
    "Col2": "5654",
    "Col3": "f644"
  },
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM