简体   繁体   中英

Write multiple lists into csv. file in Python

I am new to Python (and to programming at all). I have written a short program that reads filenames of a dedicated folder into strings. After that I 'extract' information which is in the file names (eg document number, title etc. -> later referred as value1, value 2, etc in the example).

After that I store the values into lists. One list for each file (generated with a loop) which looks like this: ['value1','value 2', 'value3']

with 'print' I get the lists displayed as I want them:

[‘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)

Now I want to store the lists into a csv.file like this:

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)

I have searched the web for possible solutions. I have tried severals things but just get the last list which was generated.

one Attempt that I have tried:

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 is the variable in which the list is defined) With this code I get:

value1
value2
value3
...

I expect that a loop is required, but I can't get my head around it.

The issue is with the lines -

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.

First of all, list2 is a 1-dimensional list of strings (according to what you have created. When using zip() directly on such lists, you get a list of tuples back, with each tuple having each element. Example -

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

You do not need to do this. Secondly, after this you use writer.writerows() , this writes each tuple in your list3 into a single line, considering each tuple as a row. You want to use writer.writerow() here . Example -

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

You can construct a list of lists which can then be passed to csv.writer.writerows() . Each of the nested lists corresponds to the values extracted from each file name; aim for a data structure like this:

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

data can be written directly to a CSV file using csv.writer,writerows(data) . Here is some code that should do what you want:

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)

This code builds up a list of lists ( data ) which is then written to a CSV file in one operation. A refinement of the code above is to use a list comprehension to create the value list for each file name and append it directly to the data list. This is more efficient and uses less code, but perhaps the first example is more understandable to you:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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