Seems like a simply thing to do, but yet I fail. I want to write the following arrays into one line in a csv file, alongside with other strings (ie "\\n"
). The values should be separated by semicolons.
I create the arrays from lists
auth_list = ['5,2.6,5.23515389636e-11,0.00444005161574,0.0644299145707,0.04,0.0037037037037', '1,5.0,5.65187364062e-12,0.0,0.0605326876513,0.0,0.000740740740741']
aff_list = [(1, 0.003105590062111801), (1, 0.001838235294117647)]
comm_list = ['', '4,17.5,0.00584085856718,0.0002890919969,0.278790504782,0.0140752484561,0.0029973357016,0.00044404973357', '0,0.0,0.000218693005322,3.33471210416e-07,0.232075228649,0.0,0.000222024866785,0.0', '0,0.0,0.00025235732634,6.73003774237e-07,0.233652374653,0.0428571428571,0.000555062166963,0.0']
I proceed turning them into arrays
import numpy
auth_array = numpy.genfromtxt(auth_list, delimiter=",")
aff_array = numpy.array(aff_list) # Here I don't have to use numpy.genfromtext, don't know why
auth_array = numpy.genfromtxt(aff_list, delimiter=",")
These arrays already have no commas. I calculate the means using
auth_mean = numpy.mean(auth_array, axis=0)
aff_mean = numpy.mean(aff_array, axis=0)
comm_mean = numpy.mean(comm_array, axis=0)
title = "Happy New Year"
Using print
, I see in the terminal
auth_mean = [ 3.00000000e+00 3.80000000e+00 2.90017063e-11 2.22002581e-03
6.24813011e-02 2.00000000e-02 2.22222222e-03]
aff_mean = [ 1. 0.00247191]
comm_mean = [ 1.33333333e+00 5.83333333e+00 2.10396963e-03 9.66994906e-05
2.48172703e-01 1.89774638e-02 1.25814091e-03 1.48016578e-04]
The arrays always have the same dimension.
output_text = title + str(auth_mean).strip('[]') + ";" + str(com_mean).strip('[]') + ";" + str(aff_mean).strip('[]') + "\n"
output_file = open(output_file_name, 'w')
output_file.write(output_text)
output_file.close()
yields
Happy New Year;3.00000000e+00 3.80000000e+00 2.90017063e-11 2.22002581e-03
6.24813011e-02 2.00000000e-02 2.22222222e-03; 1.33333333e+00 5.83333333e+00 2.10396963e-03 9.66994906e-05
2.48172703e-01 1.89774638e-02 1.25814091e-03 1.48016578e-04; 1. 0.00247191
How can I make a simple straight line like
Happy New Year;3.00000000e+00;3.80000000e+00;2.90017063e-11;2.22002581e-03;6.24813011e-02;2.00000000e-02;2.22222222e-03;1.33333333e+00;5.83333333e+00;2.10396963e-03;9.66994906e-05;2.48172703e-01;1.89774638e-02;1.25814091e-03;1.48016578e-04;1.;0.00247191
I added commas to separate the elements of your three lists (as queried by MERose above), and I also had to initialise output_file_name: output_file_name = "output.csv"
.
I hope this isn't a stupid question, but is it possible you are checking the outputted file in a text editor with "word wrap" turned on? I ran the code, and I get a single line of output as you require. If I then view the file in Notepad++ with "word wrap" turned on, it appears to be on multiple lines when actually it is just one line.
FYI: the outputted file also imports nicely into LibreOffice Calc.
First, you forgot commas in your code, I think it is:
title = "Happy New Year"
auth = [ 3.00000000e+00, 3.80000000e+00, 2.90017063e-11, 2.22002581e-03,
6.24813011e-02, 2.00000000e-02, 2.22222222e-03]
aff = [ 1. , 0.00247191]
comm = [ 1.33333333e+00, 5.83333333e+00, 2.10396963e-03, 9.66994906e-05,
2.48172703e-01, 1.89774638e-02, 1.25814091e-03, 1.48016578e-04]
Now about the problem: You can merge many arrays into one using itertools chain :
from itertools import chain
out = chain([title], auth, aff, comm, ['\n'])
Then you can join this row by semicolon:
str_row = ';'.join(map(str, out))
print(str_row) #or write to file, if you need
However, I strongly suggest you not reinvent the wheel and use standard csv module for writing any csv files. You can specify ';'
as delimiter when setting up csv writer.
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.