2016-04-25 4 views
4

Ich möchte Eingabedatei erstellen, das Format wie folgt aus (21 Zeilen und 20 Spalten)Wie schreibe ich zwei numply Arrays in Textdatei in einem richtigen Format?

0. 2900. 0. 2900. 0. 2900. 
100. 2900. 100. 2900. 100. 2900. 
200. 2900. 200. 2900. 200. 2900. 
300. 3600. 300. 3600. 300. 3600. 

Hier ist mein Code

import numpy as np 
import matplotlib as m 
import matplotlib.pyplot as plt 

# synthetic model code 
# 6 zones 

a1=np.empty(300) 
a1.fill(2900) 

a2=np.empty(400) 
a2.fill(3600) 

a3=np.empty(200) 
a3.fill(4700) 

a4=np.empty(100) 
a4[0:81]=4900 
a4[81:100]=5400 

a5=np.empty(100) 
a5[0:6]=5400 
a5[6:74]=4900 
a5[74:78]=5100 
a5[78:100]=5400 

a6=np.empty(100) 
a6[0:11]=5400 
a6[11:70]=4900 
a6[70:77]=5100 
a6[77:100]=5400 

a7=np.empty(100) 
a7[0:15]=5400 
a7[15:61]=4900 
a7[61:74]=5100 
a7[74:100]=5400 

a8=np.empty(100) 
a8[0:19]=5400 
a8[19:57]=4900 
a8[57:70]=5100 
a8[70:100]=5400 

a9=np.empty(100) 
a9[0:21]=5400 
a9[21:57]=4900 
a9[57:70]=5100 
a9[70:100]=5400 

a10=np.empty(100) 
a10[0:21]=5400 
a10[21:57]=4900 
a10[57:70]=5100 
a10[70:100]=5400 

a11=np.empty(100) 
a11[0:21]=5400 
a11[21:57]=4900 
a11[57:70]=5100 
a11[70:100]=5400 

a12=np.empty(300) 
a12.fill(5400) 

b=np.concatenate((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)) 
c = b[::5].copy() 
b1=np.reshape(c,(20,20)) 
print type(b1) 
x = np.array([0.0, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1100.0, 1200.0, 1300.0, 1400.0, 1500.0, 1600.0, 1700.0, 1800.0, 1900.0, 2000.0]) 

np.set_printoptions(threshold=np.inf, linewidth=np.inf) 
with open('velpar1.txt', 'w') as f: 
    f.write(np.array2string(b1, separator='')) 

Original-velpar1.txt

[[ 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900.] 
[ 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900.] 
[ 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900. 2900.] 
[ 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600.] 
[ 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600.] 
[ 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600.] 
[ 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600. 3600.] 
[ 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700.] 
[ 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700. 4700.] 
[ 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5400. 5400. 5400.] 
[ 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5100. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5100. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5100. 5400. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5100. 5400. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5100. 5400. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 5400. 4900. 4900. 4900. 4900. 4900. 4900. 4900. 5100. 5100. 5400. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400.] 
[ 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400. 5400.]] 

hätte Wie np.array x in die f.write-Anweisung aufnehmen?

Antwort

4

numpy hat eine großartige Funktion savetxt(), die ein Array in einer Datei in genau dem Format speichert, das Sie suchen. Ich würde vorschlagen, dies anstelle von write zu verwenden.

Hier ist ein kurzes Beispiel

sample_array = np.random.rand(3,2) 
    myfile = open('foo.out', 'wb') 
    np.savetxt(myfile, sample_array) 
Verwandte Themen