Well, let's see:
firstlinedone
: it's a variable to see whether the files are created, as this is done during the course of the processing the first line. In all other cases, file creation shall be omitted. As the variable is never set to False again, the state of
firstlindone
is fixed after the first loop. One could leave out this line and check for the pure existence of this variable and create it after the file preparation is done, but I prefer to have it explicitely.
The next three lines and the loop in principle are okay.
First in the
firstline
-block, I use
enumerate
. This return always a tuple, that is a set of two values. With
for columnumber, column in enumerate(line)
I tell Python to assign the first entry of this tuple with the name
columnumber
and the second one with the name
column
. The first value that
enumerate
gives is a number that is simply the position of the item
column
in the list with the name
line
. So, columnumber is equivalent to
line.index(column)
.
In this case, enumerate does not count the lines, but for the line, it counts the items that are stored in it. And here in particular: in the first line.
Then, for every
columnnumber
, a file is created.
Now, the filename. I had a replacement for the 'firstline'-block in
this post.There, the name of the file is derived from what is found in the variable
column
, and if an file with the end "a" is already found in the
listoffilenames
, the name is set to end with b.(Oh... how embarassing

... just now I see that I should have mentioned: it's necessary to create the list
listoffilenames
first...

. It's supposed to be done next to the creation of the
listoffiles
-list.)
It would be more 'pythonic' to use string.format() here, but I find it more difficult to read, so for the beginning I hope that this construction is okay. The same name as you are creating actually would appear from
newfilename ="Baumprobe{0}.csv".format([str(columnumber)])
(untested, however, hope that I made no mistake).
Or, if 'a' and 'b' together with the columtitle shall be the name of the new file:
newfilename ="Baumprobe{0}a.csv".format([str(column)])
or
newfilename ="Baumprobe{0}b.csv".format([str(column)])
.
The next lines are okay until an instance of csvwriter is created. I am sorry the I can't give an explanation for this, I understand this process not well enough by myself. I just accepted that this is necessary. I hope that someone else can fill this gap.
However, ths line
csvwriter = csv.writer(listoffiles[columnumber], delimiter=',')
does not write somthing itself. It just prepares the csvwriter for doing something according to the settings that are set here. And this 'something' in our case is the writing in the next line.
When the cycling through all files is done, they can be closed.
And then, not the functions are closed, but
f
- and that is the file opened in the very first line. Until now, it had to be open for reading.