将多个列表写入csv。 Python中的文件

时间:2015-09-29 08:33:48

标签: python csv

我是Python新手(也是编程人员)。我编写了一个简短的程序,将专用文件夹的文件名读入字符串。在那之后,我提取'文件名中的信息(例如文档编号,标题等 - >以后在示例中称为value1,值2等)。

之后我将值存储到列表中。每个文件的一个列表(使用循环生成),如下所示: ['value1','value 2',' value3']

用' print'我按照我的要求显示列表:

[‘value1‘, ‘value 2‘, 'value3'] (# generated from file 1)
[‘value1‘, ‘value 2‘, 'value3'] (# generated from file 2)
[‘value1‘, ‘value 2‘, 'value3'] (# generated from file 3)
[‘value1‘, ‘value 2‘, 'value3'] (# generated from file 4)
[‘value1‘, ‘value 2‘, 'value3'] (# generated from file 5)

现在我想将列表存储到csv.file中,如下所示:

value1, value2, value3, (# generated from file 1)
value1, value2, value3, (# generated from file 2)
value1, value2, value3, (# generated from file 3)
value1, value2, value3, (# generated from file 4)
value1, value2, value3, (# generated from file 5)

我在网上搜索了可能的解决方案。我已经尝试了几个东西,但只是获取生成的最后一个列表。

尝试了一次尝试:

import os
import csv

def go():
    folder = folderentry.get()  # reads path for 'folder'

    for path, subdirs, files in os.walk(folder):
        for name in files:



            searchValue1 = name.find("value1")

            if searchValue1 >= 0:
                parameter1 = "value 1"       
            else:
                parameter = "NOT FOUND!"


            searchValue2 = name.find("value2")

            if searchValue1 >= 0:
                parameter2 = "value 2"       
            else:
                parameter = "NOT FOUND!"


            searchValue3 = name.find("value3")

            if searchValue3 >= 0:
                parameter3 = "value 3"       
            else:
                parameter = "NOT FOUND!"


            list2 = []
            list2.append(parameter1)
            list2.append(parameter2)
            list2.append(parameter3)

            print(list2) # delivers the lists lik I want them


            # generate csv.file:
            with open('some.csv', 'wb') as f:
                writer = csv.writer(f)
                list3 = zip(list2)
                writer.writerows(list3)

(list2是定义列表的变量) 有了这段代码,我得到了:

value1
value2
value3
...

我希望循环是必需的,但我无法理解它。

2 个答案:

答案 0 :(得分:1)

问题在于 -

with open('some.csv', 'wb') as f:  #Using `w` mode overwrites the file everytime
    ...
    list3 = zip(list2)   #This does not do what you think it does.
    writer.writerows(list3)  #This expects a list of rows, and writes each row on a single line.

首先,list2是一个字符串的一维列表(根据您创建的内容。当直接在这些列表上使用zip()时,您会得到一个元组列表,每个元组都有一个具有每个元素的元组。示例 -

>>> zip(['asd','sdf','dfg'])
[('asd',), ('sdf',), ('dfg',)]

您不需要这样做。其次,在此之后使用writer.writerows(),这会将list3中的每个元组写入一行,将每个元组视为一行。您想在此处使用writer.writerow()。示例 -

with open('some.csv', 'ab') as f:
    writer = csv.writer(f)
    writer.writerow(list2)

答案 1 :(得分:0)

您可以构建一个列表列表,然后将其传递给csv.writer.writerows()。每个嵌套列表对应于从每个文件名中提取的值;瞄准这样的数据结构:

data = [['value1', 'value 2', 'value3'],
        ['value1', 'value 2', 'value3'],
        ['value1', 'value 2', 'value3']]

data可以使用csv.writer,writerows(data)直接写入CSV文件。这是一些应该做你想做的代码:

import os
import csv

def go():
    search_strings = ('value1', 'value2', 'value3')    # target strings to be found in file name
    data = []
    folder = folderentry.get()  # reads path for 'folder'

    for path, subdirs, files in os.walk(folder):
        for name in files:
            extracted_strings = []
            for s in search_strings:
                if s not in name:
                    s = 'NOT FOUND!'
                extracted_strings.append(s)
            data.append(extracted_strings)

    with open('some.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(data)

此代码构建一个列表列表(data),然后在一次操作中将其写入CSV文件。上面代码的改进是使用列表推导来为每个文件名创建值列表,并将其直接附加到data列表。这样效率更高,使用的代码更少,但也许第一个例子对您来说更容易理解:

import os
import csv

def go():
    search_strings = ('value1', 'value2', 'value3')    # target strings to be found in file name
    data = []
    folder = folderentry.get()  # reads path for 'folder'

    for path, subdirs, files in os.walk(folder):
        for name in files:
            data.append([s if s in name else 'NOT FOUND!' for s in search_strings])

    with open('some.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(data)