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 (e.g. 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.
list3 = zip(list2)?