如何使用列读取文本文件

时间:2015-07-29 23:27:06

标签: python python-2.7

我有一个像这样的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

如何删除这些空值

2 个答案:

答案 0 :(得分:4)

你不需要上课来做这个,只是一个功能:

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"
    ]
}

答案 1 :(得分:1)

尝试pandas

>>> 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"}'