How to save data in two columns in a text file in Python
Question:
I want to save the lists mean
and std
in a vertical column format in a Mean_Std.txt
but there is an issue with the format. I present the current and expected outputs.
import csv
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
with open("Mean_Std.txt", 'w', newline="n") as f:
wr = csv.writer(f)
wr.writerows(['Mean','Std'])
wr.writerows(zip(mean,std))
The current output is
M,e,a,n
S,t,d
41.96969696969697,0.22727272727273018
29.646464646464647,2.3263867473915276
24.444444444444443,2.836612913158948
22.424242424242426,3.2698851295313993
21.262626262626263,3.4897856988730362
20.80808080808081,4.038667556139951
20.606060606060606,4.342039357850636
20.606060606060606,4.342039357850636
20.606060606060606,4.342039357850636
20.606060606060606,4.342039357850636
The expected output is
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.326386747391527
24.444444444444443 2.836612913158948
22.424242424242426 3.2698851295313993
21.262626262626263 3.4897856988730362
20.80808080808081 4.038667556139951
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
Answers:
wr.writerows is iterating over given argument. Since String is iterable it gives you M,e,a,n
type of answear. If what you want is a header, replace wr.writerows(['Mean','Std'])
with wr.writerow(['Mean','Std'])
as it won’t be iterating.
For separation using tabs, while creating CSV writter use dialect
or delimiter
argument. For Example: wr = csv.writer(f, delimiter='t')
As suggested by SwimmingPassage you should use writerow for the first row and when opening the writer you should assign the delimiter to "t" instead of ‘,’ that is the default.
This is a working example:
import csv
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
with open("Mean_Std.txt", 'w', newline="n") as f:
wr = csv.writer(f, delimiter="t")
wr.writerow(["Mean","Std"])
wr.writerows(zip(mean,std))
I don’t think this is the best way of using csvwriter but should work
import csv
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
output = [{
'mean':mean,
'std':std
} for mean,std in zip(mean,std)
]
keys= output[0].keys()
with open("Mean_Std.csv", 'w', newline="n") as f:
dict_writer = csv.DictWriter(f, keys)
dict_writer.writeheader()
dict_writer.writerows(output)
Try this, its a work-around but it gets the output similar to what you would expect:
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
with open("Mean_Std.txt", 'w', newline="n") as f:
front_space = len(str(mean[0]))
mid_space = len(str(std[0]))
f.writelines(" " * (front_space // 2) + "Mean" + " " * (front_space // 2 + mid_space // 2) + "Stdn")
for entries in zip(mean,std):
f.writelines(f"{entries[0]}t{entries[1]}n")
The output almost matches yours:
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.3263867473915276
24.444444444444443 2.836612913158948
22.424242424242426 3.2698851295313993
21.262626262626263 3.4897856988730362
20.80808080808081 4.038667556139951
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
Change the line to:
wr.writerows([['Mean','Std']])
to get
Mean,Std
41.96969696969697,0.22727272727273018
29.646464646464647,2.3263867473915276
...
and the line:
wr = csv.writer(f, delimiter=" ")
to get:
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.3263867473915276
...
or use:
with open("Mean_Std.txt", 'w', newline="n") as f:
f.write(f"{'Mean':22}{'Std':22}n")
for m, s in zip(mean,std):
f.write(f"{str(m):22}{str(s):22}n")
to get:
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.3263867473915276
...
I want to save the lists mean
and std
in a vertical column format in a Mean_Std.txt
but there is an issue with the format. I present the current and expected outputs.
import csv
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
with open("Mean_Std.txt", 'w', newline="n") as f:
wr = csv.writer(f)
wr.writerows(['Mean','Std'])
wr.writerows(zip(mean,std))
The current output is
M,e,a,n
S,t,d
41.96969696969697,0.22727272727273018
29.646464646464647,2.3263867473915276
24.444444444444443,2.836612913158948
22.424242424242426,3.2698851295313993
21.262626262626263,3.4897856988730362
20.80808080808081,4.038667556139951
20.606060606060606,4.342039357850636
20.606060606060606,4.342039357850636
20.606060606060606,4.342039357850636
20.606060606060606,4.342039357850636
The expected output is
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.326386747391527
24.444444444444443 2.836612913158948
22.424242424242426 3.2698851295313993
21.262626262626263 3.4897856988730362
20.80808080808081 4.038667556139951
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
wr.writerows is iterating over given argument. Since String is iterable it gives you M,e,a,n
type of answear. If what you want is a header, replace wr.writerows(['Mean','Std'])
with wr.writerow(['Mean','Std'])
as it won’t be iterating.
For separation using tabs, while creating CSV writter use dialect
or delimiter
argument. For Example: wr = csv.writer(f, delimiter='t')
As suggested by SwimmingPassage you should use writerow for the first row and when opening the writer you should assign the delimiter to "t" instead of ‘,’ that is the default.
This is a working example:
import csv
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
with open("Mean_Std.txt", 'w', newline="n") as f:
wr = csv.writer(f, delimiter="t")
wr.writerow(["Mean","Std"])
wr.writerows(zip(mean,std))
I don’t think this is the best way of using csvwriter but should work
import csv
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
output = [{
'mean':mean,
'std':std
} for mean,std in zip(mean,std)
]
keys= output[0].keys()
with open("Mean_Std.csv", 'w', newline="n") as f:
dict_writer = csv.DictWriter(f, keys)
dict_writer.writeheader()
dict_writer.writerows(output)
Try this, its a work-around but it gets the output similar to what you would expect:
mean =[41.96969696969697, 29.646464646464647, 24.444444444444443, 22.424242424242426,
21.262626262626263, 20.80808080808081, 20.606060606060606, 20.606060606060606,
20.606060606060606, 20.606060606060606]
std =[0.22727272727273018, 2.3263867473915276, 2.836612913158948, 3.2698851295313993,
3.4897856988730362, 4.038667556139951, 4.342039357850636, 4.342039357850636, 4.342039357850636,
4.342039357850636]
with open("Mean_Std.txt", 'w', newline="n") as f:
front_space = len(str(mean[0]))
mid_space = len(str(std[0]))
f.writelines(" " * (front_space // 2) + "Mean" + " " * (front_space // 2 + mid_space // 2) + "Stdn")
for entries in zip(mean,std):
f.writelines(f"{entries[0]}t{entries[1]}n")
The output almost matches yours:
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.3263867473915276
24.444444444444443 2.836612913158948
22.424242424242426 3.2698851295313993
21.262626262626263 3.4897856988730362
20.80808080808081 4.038667556139951
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
20.606060606060606 4.342039357850636
Change the line to:
wr.writerows([['Mean','Std']])
to get
Mean,Std
41.96969696969697,0.22727272727273018
29.646464646464647,2.3263867473915276
...
and the line:
wr = csv.writer(f, delimiter=" ")
to get:
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.3263867473915276
...
or use:
with open("Mean_Std.txt", 'w', newline="n") as f:
f.write(f"{'Mean':22}{'Std':22}n")
for m, s in zip(mean,std):
f.write(f"{str(m):22}{str(s):22}n")
to get:
Mean Std
41.96969696969697 0.22727272727273018
29.646464646464647 2.3263867473915276
...