如何读取包含列的文本文件

2024-05-28 22:45:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个csv文件:

Model   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17
Acura   CSX/EL  MDX NSX RDX RSX/Integra Other
Alfa Romeo  145/146/147 156/159 166 4C  Brera   GIULIETTA   GTV/GT  Mito    Spider  Other
Aston Martin    DB7/DB9 DBS One-77  Rapide  Vanquish    Vantage Virage  Zagato  Other
Audi    A1  A3  A4  A5  A6  A7  A8  Q3  Q5  Q7  R8  S3/RS3  S4/RS4  S5/RS5  S6/RS6  S7/RS7  S8

其中第一列是一辆汽车的模型,其他列是该汽车的制造品。你知道吗

制造的数量是动态的。我想保存每辆车的品牌,并尝试这样做:

# -*- coding: utf-8 -*-
import csv
class CsvToJson:
    def __init__(self, csvFilePath):
        with open(csvFilePath, 'rU') as csvFile:
            spamreader = csv.reader(csvFile, delimiter= ';',
                                    quotechar = '|', dialect='excel')
            final = dict()
            for row in spamreader:
                makes = list()
                print ', '.join(row)

k = CsvToJson(csvFilePath = 'carsModelsMakes.csv')

但是我被困在如何获得专栏。你知道吗

在得到了很好的答案之后,我得到了这个结果

enter image description here

如何删除这些空值


Tags: 文件csvcsvfilemodelel汽车rowmdx
2条回答

试试^{}

>>> import pandas as pd
>>> myFile = 'test.txt'
>>> df = pd.read_csv(myFile, sep=",", header=0)
>>> df
    Model  A  B  C
0  Model1  a  b  c
1  Model2  a  b  c
2  Model3  a  b  c

获取“模型”列

>>> df['Model']
0    Model1
1    Model2
2    Model3
Name: Model, dtype: object

重新格式化为JSON

>>> df['Model'].to_json()
'{"0":"Model1","1":"Model2","2":"Model3"}'

您不需要类来完成此操作,只需要一个函数:

import csv
import json

def CsvToJson(csvFilePath):
    with open(csvFilePath, 'rU', newline='') as csvFile:
        final = {}
        reader = csv.reader(csvFile, delimiter='\t') # change delimiter if needed
        next(reader)  # skip header
        for row in reader:  # now removes "empty" row values
            final[row[0]] = [value for value in row[1:] if value]

    return json.dumps(final, indent=4)

k = CsvToJson('carsModelsMakes.csv')
print(k)

输出:

{
    "Acura": [
        "CSX/EL",
        "MDX",
        "NSX",
        "RDX",
        "RSX/Integra",
        "Other"
    ],
    "Aston Martin": [
        "DB7/DB9",
        "DBS",
        "One-77",
        "Rapide",
        "Vanquish",
        "Vantage",
        "Virage",
        "Zagato",
        "Other"
    ],
    "Audi": [
        "A1",
        "A3",
        "A4",
        "A5",
        "A6",
        "A7",
        "A8",
        "Q3",
        "Q5",
        "Q7",
        "R8",
        "S3/RS3",
        "S4/RS4",
        "S5/RS5",
        "S6/RS6",
        "S7/RS7",
        "S8"
    ],
    "Alfa Romeo": [
        "145/146/147",
        "156/159",
        "166",
        "4C",
        "Brera",
        "GIULIETTA",
        "GTV/GT",
        "Mito",
        "Spider",
        "Other"
    ]
}

相关问题 更多 >

    热门问题