The Python documentation states that for the csv.writer()
function the ...
"optional
dialect
parameter can be given which is used to define a set of parameters specific to a particular CSV dialect".
... and that ...
"the other optional
fmtparams
keyword arguments can be given to override individual formatting parameters in the current dialect".
The problem you are experiencing is a consequence of writing a string representation of the time
list to file and writing to file using a whitespace delimiter. If you were to view Informationen.csv
as a plain text file the problem becomes apparent.
Firstly, writing to file having passed a whitespace delimiter as an argument in csv.writer(myfile, dialect='excel', delimiter=' ')
overrides the default delimiter as defined in the Excel dialect and results in the elements of list bla
being written to file with the format element1 element2 element3
as opposed to element1,element2,element3
.
Secondly, although the majority of the elements in the time
list are allocated their own columns in a spreadsheet as desired, writing the list to file as a string representation of itself has contributed to the overall undesired formatting.
When you open the file created with your script as an Excel file, Excel reads in two initial values based on the first two commas it finds which happen to be in the center of the string 'Landhaus , Nord'
and within the string representation of the time
list.
You can achieve the column separation you require firstly by appending the elements of the time
list to the bla
list, as opposed to nesting the former within the latter. You then need to omit delimiter=' '
in csv.writer(myfile, dialect='excel', delimiter=' ')
, thus avoiding the delimiter overriding effect when writing to file:
import csv
car = ['car 11']
finish = ['Landhaus , Nord']
time = ["['05:36']", "['06:06']", "['06:36']", "['07:06']", "['07:36']"]
try:
with open('Informationen.csv', 'w') as myfile:
writer = csv.writer(myfile, dialect='excel')
bla = [car, finish]
for each_time in time:
bla.append(each_time)
writer.writerow(bla)
except IOError as ioe:
print('Error: ' + str(ioe))
producing the following output in Excel: