簡體   English   中英

Python:如何正確地將沒有標頭的 CSV 轉換為 JSON

[英]Python: How to correctly convert CSV without headers to JSON

我必須將很多 CSV 文件轉換為 JSON 文件。 正如您在下面看到的,沒有標題。 我已經設法為此創建代碼:

import gspread
import os
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload


import csv
import json
import pandas as pd
from pathlib import Path


#Odczyt pliku CSV
def read_CSV(file, json_file):
    csv_rows = []
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        field = reader.fieldnames
        for row in reader:
            csv_rows.extend([{field[i]:row[field[i]] for i in range(len(field))}])
        convert_write_json(csv_rows, json_file) #definicja funkcji ponizej

#Zamiana CSV na JSON
def convert_write_json(data, json_file):
    with open(json_file, "w") as f:
        f.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': '))) 
        f.write(json.dumps(data))

#pętla w folderze
pliki = "/users/user/CSVtoGD/"

files = Path(pliki).glob('*.csv') 

for f in files:
    read_CSV(f, str(f.with_suffix('.json'))) 

CSV 類似於以下示例:


Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Bold_0.ttf,Roboto,Bold,Roboto Bold,Roboto-Bold,Version 2.137; 2017,Roboto Bold,Google,Christian Robertson,Google.com,Roboto is a trademark of Google.,Copyright 2011 Google Inc. All Rights Reserved.,http://www.apache.org/licenses/LICENSE-2.0,,GOOG
Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Medium_0.ttf,Roboto Medium,Regular,Roboto Medium,Roboto-Medium,Roboto,,Version 2.137; 2017,Roboto Medium,Google,Christian Robertson,Google.com,Roboto is a trademark of Google.,Copyright 2011 Google Inc. All Rights Reserved.,http://www.apache.org/licenses/LICENSE-2.0,,GOOG
(...)

但結果並不十分壯觀:

[
    {
        "Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Bold_0.ttf": "Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Medium_0.ttf",
        "Roboto": "Roboto Medium",
        "Bold": "Regular",
        "Roboto Bold": "",
        "Roboto-Bold": "Roboto-Medium",
        "Version 2.137; 2017": "Roboto",
        "Google": "Version 2.137; 2017",
        "Christian Robertson": "Roboto Medium",
        "Google.com": "Google",
        "Roboto is a trademark of Google.": "Christian Robertson",
        "Copyright 2011 Google Inc. All Rights Reserved.": "Google.com",
        "http://www.apache.org/licenses/LICENSE-2.0": "Roboto is a trademark of Google.",
        "": "Copyright 2011 Google Inc. All Rights Reserved.",
        "GOOG": "http://www.apache.org/licenses/LICENSE-2.0"
    }
][{"Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Bold_0.ttf": "Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Medium_0.ttf", "Roboto": "Roboto Medium", "Bold": "Regular", "Roboto Bold": "", "Roboto-Bold": "Roboto-Medium", "Version 2.137; 2017": "Roboto", "Google": "Version 2.137; 2017", "Christian Robertson": "Roboto Medium", "Google.com": "Google", "Roboto is a trademark of Google.": "Christian Robertson", "Copyright 2011 Google Inc. All Rights Reserved.": "Google.com", "http://www.apache.org/licenses/LICENSE-2.0": "Roboto is a trademark of Google.", "": "Copyright 2011 Google Inc. All Rights Reserved.", "GOOG": "http://www.apache.org/licenses/LICENSE-2.0"}]

我不知道如何對 JSON 文件中的信息進行排序。 如您所見,CSV 只是每一行中一個 TTF 的數據。 (例如家庭、供應商等)

我不知道你期望什么結果,但你可以簡單地添加自己的帶有標題的列表

fields = ["Family", "Vendor", "Other1", "Other2", "Other3", ...]

代替

field = reader.fieldnames

最少的工作代碼。

我使用io只在內存中創建文件,所以每個人都可以簡單地復制它,但你應該使用open()

我只發現一個問題。 您的示例行在不同的列中有一些值 - 即。 版本。

data = '''Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Bold_0.ttf,Roboto,Bold,Roboto Bold,Roboto-Bold,Version 2.137; 2017,Roboto Bold,Google,Christian Robertson,Google.com,Roboto is a trademark of Google.,Copyright 2011 Google Inc. All Rights Reserved.,http://www.apache.org/licenses/LICENSE-2.0,,GOOG
Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Medium_0.ttf,Roboto Medium,Regular,Roboto Medium,Roboto-Medium,Version 2.137; 2017,Roboto Medium,Google,Christian Robertson,Google.com,Roboto is a trademark of Google.,Copyright 2011 Google Inc. All Rights Reserved.,http://www.apache.org/licenses/LICENSE-2.0,,GOOG
'''

import csv
import json
import io

# --- load CVS ---

input_filename  = 'input.csv'
output_filename = 'output.json'

fields = ["Family", "Vendor", "Other1", "Other2", "Other3", "Other4", "Other5", "Other6", "Other7"]
csv_rows = []

#with open(input_filename) as fh_in:
with io.StringIO(data) as fh_in:
    reader = csv.reader(fh_in)
    for row in reader:
        dictionary = dict(zip(fields, row))
        csv_rows.append(dictionary)

# --- write JSON in file ---

with open(output_filename, "w") as fh_out:
    json.dump(csv_rows, fh_out, indent=4)

# --- display JSON ---

print(json.dumps(csv_rows, indent=4))

結果:

[
    {
        "Family": "Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Bold_0.ttf",
        "Vendor": "Roboto",
        "Other1": "Bold",
        "Other2": "Roboto Bold",
        "Other3": "Roboto-Bold",
        "Other4": "Version 2.137; 2017",
        "Other5": "Roboto Bold",
        "Other6": "Google",
        "Other7": "Christian Robertson"
    },
    {
        "Family": "Assets/Lakeside/Resources/Graphics/font_ttf/Roboto-Medium_0.ttf",
        "Vendor": "Roboto Medium",
        "Other1": "Regular",
        "Other2": "Roboto Medium",
        "Other3": "Roboto-Medium",
        "Other4": "Version 2.137; 2017",
        "Other5": "Roboto Medium",
        "Other6": "Google",
        "Other7": "Christian Robertson"
    }
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM