Armin
Armin

Reputation: 361

How to create files which are named by the elements of a column stored in a file?

I have a file which contains two columns, the first one contains the name and the second one contains the measurements for the corresponding names.

I need to create files with the name of the measurements, and each measurement has the corresponding measurement for it.

In other words, I need to make files which are named by the first column and they contain the element of the second column (but the same line).

I thought of the following code:

import numpy as np
arr=np.genfromtxt('file.txt', dtype=(str))
arr_0=arr[:,0]
arr_1=arr[:,1]
numrows = len(arr)
for i in range (0,6):
    name=arr_0[i]
    value=arr_1[i]
    np.savetxt(name, int(value))
    i=i+1

My code does not work, can you tell me what should be changed and why?

FYI, I tried np.savetxt(name,value) and this did not work either!

The file:

el02_f125lp_wht_r.fits  520758208.
el02_f140lp_wht_r.fits  758538560.
el02_f150lp_wht_r.fits  1.030201E9
el02_f336w_wht_r.fits  30627980.
el02_f680n_wht_r.fits  18258366.
el02_f775w_wht_r.fits  3536094.
el02_fq508n_wht_r.fits  58293324.

Upvotes: 0

Views: 40

Answers (1)

Andrew
Andrew

Reputation: 750

This code works...

import numpy as np

arr = np.genfromtxt('file.txt', dtype=str)

for i in range(0, len(arr)):
    np.savetxt(arr[i, 0], [float(arr[i, 1])])   # int -> float

Upvotes: 1

Related Questions