简体   繁体   English

将列表列表写入CSV,但仅获取密钥

[英]Writing a list of lists to a CSV, but only getting keys

I have about 60 CSVs, each with 4 commons values that I need to extract and turn into one CSV. 我大约有60个CSV,每个都有4个通用值,我需要提取这些值并将它们转换为一个CSV。 I've cut out a lot of info here, but confirmed that 'output_contents' has all the correct information, but it isn't being written when I call 'create_csv'. 我在这里剪掉了很多信息,但是确认'output_contents'具有所有正确的信息,但是当我调用'create_csv'时并没有编写该信息。

        
def create_csv(list_of_lists):
    '''
    Writes the list of lists to a actual CSV file.

    PARAMS:
    list_of_lists - A list of keys, and each key is a list of values.

    RETURNS: None.

    OUTPUTS: A CSV file named "output.csv".
    '''
    print "Attempting to write CSV."
    with open("output.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(list_of_lists.keys())
        print "Write successful."

fileList = get_all_files(csv_directory)
get_csv_contents(fileList)

# Copy over the columns from the contents dictionary.
wanted_columns = ["key1", "key2", "key2", "key4",]

# Creates a key: value pair for every key and value in content as long as the key is a wanted column.
output_contents = {key: value for key, value in content.items() if key in wanted_columns}

create_csv(output_contents)

I've confirmed that output_contents has all the information from the input CSVs that is should. 我已经确认output_contents具有应该输入的CSV中的所有信息。

When I run this, my output.csv looks like: 当我运行它时,我的output.csv看起来像:


k,e,y,1
k,e,y,2
k,e,y,3
k,e,y,4

I know I'm making some small, silly error somewhere, but think my brain is fried and can't figure out where the hiccup is. 我知道我在某个地方犯了一些小的愚蠢错误,但认为我的大脑已炸裂,无法弄清楚打ic的位置。

EDIT: 编辑:

Here's runnable code. 这是可运行的代码。

import csv

def create_csv(list_of_lists):
    '''
    Writes the list of lists to a actual CSV file.

    PARAMS:
    list_of_lists - A list of keys, and each key is a list of values.

    RETURNS: None.

    OUTPUTS: A CSV file named "output.csv".
    '''
    print "Attempting to write CSV."
    with open("output.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(list_of_lists.keys())
        print "Write successful."



output_contents = {
       'key1': ["k1v1","k1v2","k1v3"],
       'key2': ["k2v1","k2v2","k2v3"],
       'key3': ["k3v1","k3v2","k3v3"],
       'key4': ["k4v1","k4v2","k4v3"],}

create_csv(output_contents)

The writerows method expects a list of iterables but you're providing a list of strings (which causes the function to iterate the string and consider each character as a value). writerows方法需要一个可迭代的列表,但您要提供一个字符串列表(这会使函数迭代该字符串并将每个字符视为一个值)。 So you should use instead... 所以你应该改用...

output_contents = ((key, value) for key, value in content.items() if key in wanted_columns)

And in the create_csv function... 然后在create_csv函数中...

writer.writerows(list_of_lists)

Hope it helps! 希望能帮助到你!

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

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