我真的很陌生,我無法做一些非常簡單的事情。我在這裏看到了一些用於處理字符串和數字的例子,但不知道如何處理幾行後跟數組數組的一個字符串頭。我找到了一個部分解決方案,但是第一行數據被部分截斷。這是我所做的:閱讀/書寫標題字符串和數字數組
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()
以上幾乎都有效。這只是 第一行數據的前半部分丟失。
任何幫助將不勝感激。
感謝,
你還應該包括infile.txt'的'一個片段,使我們可以看到的數據是什麼樣子。 – jme
好點。我介紹下面的示例行。我用\ n來表示輸入文件中的新行。 //線的數目: // v 1215 \ N:512.0 \ n X1 X2 X3 X4 X5 X4 6 X 7 \ n 73156.727567363 \t \t 23982.109 0.000000000 \t \t 0.0 -0.0009 \t \t 31.353455648 128.485 \ n 73188.081023010 \t 23863.683 \t \t 0.000000000 0.0 \t \t -0.0006 31.303370255 \t 128.225 \ n 73219.334307873 \t \t 23745.635 0.000000000 \t \t 0.0 -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 –