2016-07-19 5 views
0

私はPythonには本当に新しく、とても簡単なことはできません。文字列と数値を扱う場合の例を見ていますが、数行の文字列ヘッダとそれに続く数値の配列を扱う方法ではありません。私は部分的な解決策を見つけましたが、データの最初の行が部分的に切り刻まれています。これは私がやったことです:ヘッダー文字列と数値配列の読み書き

import numpy as np 
infil="infile.txt" 
outfil="outfile.txt" 
fi = open(infil,"r") 
fo = open(outfil,"w") 
lines = fi.readlines() 
fo.writelines(lines[0:3]) 
fi.close() 
x1, x2, x3, x4, x5, x6, x7 = np.loadtxt(infil, skiprows=3, unpack=True) 
# Some computations with those columns of numbers occurs here. 
data = np.array([x1,x2,x3,x4,x5,x6,x7] 
data = data.T 
np.savetxt(outfil, data, fmt='%f') 
fo.close()  

上記もほぼ同じです。最初の行のデータの最初の半分が欠落しているだけです。

ご協力いただきますようお願い申し上げます。

おかげで、 ビル

+0

あなたはまた、infile.txt' 'のスニペットを含める必要があります。これであなたは本当にその一時ファイルは必要ありません。 – jme

+0

良い点。私は以下のサンプルラインを提示します。私は\ nを使って入力ファイルに新しい行を表します。 //行数:1215 \ nは // V:512.0 \ nは ×1×2×3×4×4×5×6×7の\ nは 73156.727567363 \t 23982.109 \t 0.000000000 \t 0.0 \t -0.0009 \t 31.353455648 \t 128.485 \ nは 73188.081023010 \t 23863.683 \t 0.000000000 \t 0.0 \t -0.0006 \t 31.303370255 \t 128.225 \ n 73219.334307873 \t 23745.635 \t 0.000000000 \t 0.0 \t -0.0008 \t 31.303370255 \t 128.170 \ nは 73250.687763521 \t 23627.209 \t 0.000000000 \t 0.0 \t -0.0006 \t 31.303370255 \t 128.115 \ nは –

答えて

0

私は多分、よりエレガントな解決策を追加したい: np.savetxtは前のデータをファイルに書き込まれたヘッダ文字列を受け付けます。我々は、データがどのように見えるか見ることができるように

np.savetxt(filename, data, header="my header\nand a second line")

+0

ありがとう!はい、savetxtがそのオプションを持つことは確かに意味があります。より洗練されたソリューションが待っています.... –

+0

さて、私は最終的にあなたが提案する解決策を試してみました。各行が "#"で始まり、余分な空白行( "#"で始まる)があるヘッダーを作成します。私はそれを望んでいない。 np.savetxt(outfil、data、fmt = '%f \ t'、ヘッダー= h1 + h2 + h3.rstrip( '\ n')、コメントは次のように書かれている必要があります。 '')これは、私が望むヘッダーを書き出し、その直後にデータの行が続きます。あなたの助けをもう一度ありがとう。 –

+0

OK、ヘッダをマークしたくないとは気づきませんでした。それがあなたのために働くなら、あなたは私の答えを受け入れたものとしてマークすることを検討したいかもしれません – Dux

0

はい!それを解決!その解決策は面白いですが。問題は、3行のヘッダーの後に適切にフォーマットされたデータの多くの行を含む出力ファイルを持つことでした。最初の1.5行のデータは上記のアルゴリズムで切断されました。私が以下に示す解決策は機能しますが、一時ファイルを使用して書式設定されたデータ行を最終出力ファイルに書き込む文字列に渡すため、エレガントではありません。このアルゴリズムを改善し、改良するための提案は感謝しています。それまでは、これはうまく動作します。

import numpy as np    # Load useful routines for arrays. 

infil="infile.txt"    # Input file name. 
tfil ="tmp.txt"     # Temporary file name. 
outfil="outfile.txt"   # Output file name. 

fi = open(infil,"r")   # Open input file. 
ft = open(tfil,"w")    # Open temporary file for writing into. 
fo = open(outfil,"w")   # Open output file. 

lines = fi.readlines()   # Read in the input file. 

fi.close()      # Close input file. 




x1, x2, x3, x4, x5, x6, x7 = np.loadtxt(infil, skiprows=3, unpack=True) 
          # Read in and unpack the numerical arrays from the input file. 

# Computation with those numerical arrays will occur here when I'm ready.        


data = np.array([x1,x2,x3,x4,x5,x6,x7]) 
          # Pack the data arrays together. 

data = data.T     # Transpose the data into columns. 


np.savetxt(tfil, data, fmt='%f\t')  # Write to temporary file. 


ft.close()      # Close temporary file. 

ft = open(tfil,"r")    # Now open temporary file to read in the formatted data lines. 

dlines = ft.readlines()   # Read in the temporary file. 

fo.writelines(lines[0:3])  # Write the header to the output file. 

fo.writelines(dlines)   # Write the formatted data lines to the output file. 

fo.close()      # Close output file. 
関連する問題