Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 (i.e. "\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
share|improve this question
    
How come their are no commas in your arrays? –  PM 2Ring Jan 3 at 9:51
    
No idea. I create the arrays with numpy.mean(.. , axis=0) from arrays that I create from lists using numpy.genfromtxt(.., delimiter=",") The lists of course are comma-separated, but the arrays aren't. –  MERose Jan 3 at 9:58
    
In that case, you should add the numpy tag to your question so that numpy experts will notice it. (I Am Not A Numpy Expert). –  PM 2Ring Jan 3 at 10:00
    
Is done. Could really be the case the numpy conversion is the problem. –  MERose Jan 3 at 10:20

2 Answers 2

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.

share|improve this answer
    
No, I didn't forgot the commas. I copied the output right from the terminal. But I will check out itertools. –  MERose Jan 3 at 10:12
    
Exactly. You copied it somewhere from terminal output , not from code source –  Ivan Klass Jan 3 at 10:38
    
The last code block (after "yields") is from the csv file. –  MERose Jan 3 at 10:40

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.

share|improve this answer
    
Wrod wrap is off. I really get a multiple line output. How did you add commas to separate the lists? –  MERose Jan 3 at 10:21
    
I just added them in manually. Sorry I jumped the gun slightly with my answer - I answered before you posted that the arrays originated from numpy, whereas I had assumed they were standard python lists. –  Paul Douglas Jan 3 at 10:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.