如何从python2.7中的元组列表创建csv文件?

2024-06-12 05:34:48 发布

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

我有一个元组列表,我正试图找出如何创建一个csv文件,其中键作为列标题,值作为列值。在

下面是元组的示例列表:

[('d_conversion_rate', 1), ('prev_2wk_visit_count', 0.0), ('sku_id', '100088-01'), ('prev_1wk_uniq_purch_cnt', 0.0)]
[('d_conversion_rate', 0), ('prev_2wk_visit_count', 6.0), ('sku_id', '100088-02'), ('prev_1wk_uniq_purch_cnt', 0.0)] 
[('d_conversion_rate', 5), ('prev_2wk_visit_count', 7.0), ('sku_id', '100088-03'), ('prev_1wk_uniq_purch_cnt', 0.0)]

预期的csv文件应具有:

^{pr2}$

这是我写的代码:

import os
import sys
import csv
import string
import random


def import_data_to_csv(dict_d):
    with open('C:/Reports/SI_Reconciliation_Reporting/2015/output/2015-04-19/test_dump.csv', 'w') as outfile:
        fp = csv.DictWriter(outfile, dict_d[0].keys())
        fp.writeheader()
        fp.writerows(dict_d)



for row in sku_weekL:
                #print row.items()
                for key, value in row.items():
                    sku_weekTemp.append((key, value))
                print sku_weekTemp

            #print sku_weekL
            #print sku_weekL
            #print dir(sku_weekL)
            import_data_to_csv(sku_weekTemp)

但是运行脚本时,我得到一个错误:tuple对象没有属性键。在

有什么办法解决这个问题吗?在


Tags: csvimportidratecountvisitdictprint
2条回答

根据我对你问题的理解,这里有一个解决方案。 如果有任何问题可以问。我个人不喜欢python标准库csv,在需要编写csv文件时,总是没有它。在

def asDic(listOfTuple):
    """ Convert a list of tuple [(key,value)] into a dictionary """
    return {i:j for (i,j) in listOfTuple}

def exportLine(lineMembers, outputFile, separator):
    """
    exportLine(["lineMembers"foo", "bar", "foo"], file, "separator",")
        -> file.write("foo, bar, foo\n")
    """
    lineString = ""
    for i in range(len(lineMembers)-1):
        lineString += str(lineMembers[i]) + separator
    #Append the last element without a separator, instead add a end of line
    lineString += str(lineMembers[-1]) + "\n"
    outputFile.write(lineString)

def exportCSV(data, headers, outputFile, separator = ", "):
    """
    Exports the data into the outputFile using the given headers.
    It is possible to generate any separated value file by overriding the default separator.

    data : a list of dict object. Their content will be accesed using the values of headers as keys.
    """
    #export the headers
    exportLine(headers, outputFile, separator)

    for dataset in data : 
        #Build the data line
        exportData = [ dataset[header] for header in headers ]
        #and export it
        exportLine(exportData, outputFile, separator)


if __name__ == "__main__":

    row1 = [('d_conversion_rate', 1), ('prev_2wk_visit_count', 0.0), ('sku_id', '100088-01'), ('prev_1wk_uniq_purch_cnt', 0.0)]
    row2 = [('d_conversion_rate', 0), ('prev_2wk_visit_count', 6.0), ('sku_id', '100088-02'), ('prev_1wk_uniq_purch_cnt', 0.0)]
    row3 = [('d_conversion_rate', 5), ('prev_2wk_visit_count', 7.0), ('sku_id', '100088-03'), ('prev_1wk_uniq_purch_cnt', 0.0)]

    data = [row1, row2, row3]

    #get the list of headers that will be exported
    columnHeader = [i for (i,j) in data[0]]
    #transform the data to allow access them by field
    data = [asDic(row) for row in data]

    #you may add here whatever code you want in order to sort/filter/remove some headers here.
    #Removing one header will remove the column from the final export.
    columnHeader.pop(1)#commenting this will add the column prev_2wk_visit_count to the file.

    outputFile = open("./output.csv", 'w')
    exportCSV(data, columnHeader, outputFile)
    outputFile.close()

错误试图告诉您dict_d[0]是一个元组对象,因此没有keys()方法(只有dict有这个方法)。在

首先,您应该按您的首选顺序列出列名:

column_names = ['d_conversion_rate', 'sku_id', 'prev_1wk_uniq_purch_cnt']

由于行已经是具有这些键的dict,所以不需要转换它们。在

当您有了所有的value\u行时,您可以将它与列名称一起使用来编写它:

^{pr2}$

编辑:我没注意到你用的是DictWriter,所以把dicts转换成列表是不必要的

edit2:示例:

val_rows这样

val_rows = [
    {'sku_id': 1, 'd_conversion_rate': 2, 'prev_1wk_uniq_purch_cnt': 3},
    {'sku_id': 4, 'd_conversion_rate': 5, 'prev_1wk_uniq_purch_cnt': 6}
]

你可以像这样使用上面的函数

col_names = ['d_conversion_rate', 'sku_id', 'prev_1wk_uniq_purch_cnt']
import_data_to_csv(val_rows, col_names)

# or dynamically

import_data_to_csv(val_rows, val_rows[0].keys())

如果您的数据没有格式化为dict列表,那么您必须进行转换。从上面的问题中获取元组列表,我们可以将其转换为如下所示的dict列表

list_of_list_of_tuples = [
    [('d_conversion_rate', 1), ('prev_2wk_visit_count', 0.0), ('sku_id', '100088-01'), ('prev_1wk_uniq_purch_cnt', 0.0)],
    [('d_conversion_rate', 0), ('prev_2wk_visit_count', 6.0), ('sku_id', '100088-02'), ('prev_1wk_uniq_purch_cnt', 0.0)],
    [('d_conversion_rate', 5), ('prev_2wk_visit_count', 7.0), ('sku_id', '100088-03'), ('prev_1wk_uniq_purch_cnt', 0.0)]
]

val_rows = [{tup[0]: tup[1] for tup in row} for row in list_of_list_of_tuples]

希望现在一切都清楚了。在

编辑3: 你在评论里写的东西漏掉了一些信息,所以我得猜测一下

for i in List:
    sku_L = list()  # I'm assuming you're appending each sku_L to another list somewhere further down
    for row in insL:  # I'm assuming insL is calculated in the outer loop or is just i
        insertD = dict()
        # here I assign all the values to the keys of the dictionary
        sku_L.append(insertD)
        pprint(sku_L)

您不希望每个字典都有一个新列表,所以我将从循环中拉出列表实例化。pprint的目的是在没有循环的情况下使用它,因此您可以看到整个过程。在

sku_L = list()  # only one list now
for i in List:
    for row in i:  # insL is probably just i
        insertD = dict()
        # here I assign all the values to the keys of the dictionary
        sku_L.append(insertD)

pprint(sku_L)  # printing the whole thing should be a list of dicts

我很确定你的数据实际上只是一个记录列表,每个记录都有固定位置的数据。如果是这种情况,你可以简化很多:

input_list = [  # this is a list of records
    [1, 0.0, 0.0, '100088-01'],
    [1, 0.0, 6.0, '100088-02']
]

sku_L = list()
for row in input_list:
    insertD = {
        'd_conversion_rate': row[0],
        'prev_2wk_visit_count': row[1],
        'prev_1wk_uniq_purch_cnt': row[2],
        'sku_id': row[3]
    }
    sku_L.append(insertD)

pprint(sku_L)

相关问题 更多 >