-2

The arrays are as below

array1=np.array([1.5397e-05,8.7383e+00,2.6633e+01,1.1309e+03,4.3194e+02,2.5086e+01])
array2=np.array([4.83,1.4,0.4,-7.2,-3.64,0.6])
array3=([‘Sun’,’Sirius’,’Arcuturus’,’Betelgeuse’,’Polaris’,’Vega’])

I would like the data to look like the information below in a text file called star.txt.

Sun         1.5397e-05   4.83
Sirius      8.7383e+00   1.4
Arcuturus   2.6633e+01   0.4
Betelgeuse  1.1309e+03   -7.2
Polaris     4.3194e+02   -3.64
Vega        2.5086e+01   0.6

Can anyone please help?

Kathryn Parr
  • 21
  • 1
  • 2
  • Look at the `zip` built-in function https://docs.python.org/3/library/functions.html#zip – cdarke Aug 07 '16 at 14:15
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Aug 07 '16 at 14:16
  • 1
    Maybe this will help you get started: http://stackoverflow.com/questions/16621351/how-to-use-python-numpy-savetxt-to-write-strings-and-float-number-to-an-ascii-fi – pathoren Aug 07 '16 at 14:19
  • Please don't use "smart-quotes" when you post code. – cdarke Aug 07 '16 at 14:20
  • Sorry I will add which coding I have tried. – Kathryn Parr Aug 07 '16 at 14:30

3 Answers3

2

Disclaimer: as soon as you are already using NumPy i would use vectorized approach, i.e. (no loops, use NumPy's or pandas's power to do it for you)

You can use np.savetxt() function as @pathoren has mentioned in the comment:

np.savetxt('c:/temp/out.csv', np.array([array3, array1, array2]).T, delimiter='\t', fmt="%s")

or using pandas module:

import pandas as pd

pd.DataFrame({'col1':array3,'col2':array1,'col3':array2}).to_csv('c:/temp/out.csv', index=False, header=None, sep='\t')

NOTE: i would NOT recommend you to use space/TAB delimited text as it might cause problems in future (for example when you will need to read/parse this file and if you will have stars that have multiple words in their names)

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • Thank you very much. I had tried different variations of np.savetxt and used pd.dataframe but could only seem to get the file to be printed the wrong way around with the array3 in the middle not the beginning. – Kathryn Parr Aug 07 '16 at 14:51
0

It's so simple it hardly worth pointing out that you should really be able to work this out yourself. I suggest you follow a basic Python tutorial, there are plenty on the web.

There are several solutions, here is mine:

import numpy as np

array1=np.array([1.5397e-05,8.7383e+00,2.6633e+01,1.1309e+03,4.3194e+02,2.5086e+01])
array2=np.array([4.83, 1.4, 0.4, -7.2, -3.64, 0.6])
array3=(['Sun','Sirius','Arcuturus','Betelgeuse','Polaris','Vega'])

with open('star.txt', 'w') as fh:
    for a,b,c in zip(array1, array2, array3):
        print("%-10s  %e  %05.2f" % (c, a, b), file = fh)

That assumes Python 3.

cdarke
  • 42,728
  • 8
  • 80
  • 84
0

How's this?

import numpy as np

array1=np.array([1.5397e-05,8.7383e+00,2.6633e+01,1.1309e+03,4.3194e+02,2.5086e+01])
array2=np.array([4.83,1.4,0.4,-7.2,-3.64,0.6])
array3 = ['Sun','Sirius','Arcuturus','Betelgeuse','Polaris','Vega']

with open('star.txt', 'w') as f:
    for a, b, name in zip(array1, array2, array3):
        f.write('{0:15}{1:15}{2:15}\n'.format(name, a, b))

Output

File star.txt in same folder with the following contents:

Sun                 1.5397e-05           4.83
Sirius                  8.7383            1.4
Arcuturus               26.633            0.4
Betelgeuse              1130.9           -7.2
Polaris                 431.94          -3.64
Vega                    25.086            0.6

For the record it would be nice if you used more descriptive variable names than array1, array2, etc. so that I could use more descriptive variable names than a, b, etc.

Tagc
  • 8,736
  • 7
  • 61
  • 114