-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?

5
  • Look at the zip built-in function docs.python.org/3/library/functions.html#zip Commented Aug 7, 2016 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 and How to Ask. Commented Aug 7, 2016 at 14:16
  • 1
    Maybe this will help you get started: stackoverflow.com/questions/16621351/… Commented Aug 7, 2016 at 14:19
  • Please don't use "smart-quotes" when you post code. Commented Aug 7, 2016 at 14:20
  • Sorry I will add which coding I have tried. Commented Aug 7, 2016 at 14:30

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

Thank you very much this really helped sorry I forgot to list code I had already tried.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.