2013-02-01 52 views
-1

我正在使用python33並想從包含單詞和數字的文本文件(下面的複製/粘貼的txt文件中的第30行)獲取一串數字。捕獲所需的數字後,我想用公式(即(數字-30)/ 2)轉換它們,然後找到它們的平均值並將其寫入新文件(例如Scope22.txt或csv)。我有許多單獨的文件,第一個文件必須進行類似的處理,每個文件的平均值將寫入同一文件(Scope22.txt或csv)。附加文件包含concersn(0.0,66.0,70.0,0.0,0.0,81.0,64.0,0.0,76.0)的數量....將它們轉換((number-30)/ 2),然後找到它們的平均值。
我真誠感謝您的幫助。使用文本文件的Python

My.txt文件:

 
MinorType Scope 
ValidTime 201012132300 
Range 536 
Theta 48 
ID SIT 
Name SITE 
LatCentre 23.45459 
LonCentre -22.766379 
Resolution1 1.0 
Resolution1 1.0 
Height1 11.0 
Height3 600.0 
Data index 
InputFileType AAA,0,1,PPTOA 
MinorProductParameters SCOPe,,BBBBBBB,KKKKK_LIST_SIT,0,0,1,1 
TableLabels_Locations ID Name Valid Lat Lon Range Theta BBN Yyy 
TableStart_SiteLocations 
"LLL", "QQQQ_WWWWW", 1, 23.45459,-22.766379, 536.11,48.14, 55,22; 
TableEnd_SiteLocations 
TableLabels_TableInfoSites ID Lat Lon Range Theta BMHT PNTVlU Min Max Avg Median P95 AbsDev Var Sd Skew Curt 
TableStart_TableInfoSites 
LLL, 23.45459,-22.766379, 536.11,48.14, -1.00, 0.00, 0.00, 81.00, 39.67, 64.00, 0.00, 35.26,1.44e+03, 37.96, -0.14, -2.13; 
TableEnd_TableInfoSites 
TableLabels_TableDis Dis M 
TableStart_TableDis 
LLL, 4,200,190,180,170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,; 
TableEnd_TableDis 
TableLabels_TableData Values_M 
TableStart_TableData 
LLL, 0.0, 66.0, 70.0, 0.0, 0.0, 81.0, 64.0, 0.0, 76.0,; 
TableEnd_TableData 

這裏是我的代碼:

STATS11_filename='C:/Python33/STATS11.txt' 
STATS11_filename=open(STATS11_filename,'r') 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline() 
line=STATS11_filename.readline()  
line=STATS11_filename.readline() 
b=line=STATS11_filename.readline() 
d=(str(b[7:-3])) 
print(len(b[7:-3])) 
STATS11_filename.close() 
a=7 
Scope22_filename='C:/Python33/Scope22.txt' 
Scope22_filename=open(Scope22_filename,'w') 
for i in range (len(b[7:-3])): 
    if (b[a])>='64' or (b[a])<='254': 
     Scope22_filename.write(b[a])  
      if a==53: 
       quit 
      else: 
       a=a+2 
Scope22_filename.close() 
+6

看來您希望我們爲您編寫代碼。這不是如何工作。你發佈了你想做的事情,並且幫助你發現問題。 – StoryTeller

+0

也不代表太。我已將代碼添加到主要問題中。我可以在屏幕上打印數字,但無法將它們寫入文件中,因爲我需要它們。我試過沒有運氣。 – user2031063

回答

0

你的變量b是一個字符串(b等於「0.0,66.0,70.0,0.0,0.0,81.0,64.0 ,0.0,76.「在你的例子中)並且你的變量是一個int(一個等於7),那麼b [a]將只引用一個字符,這不能是'64'(兩個字符長)。

我想你試圖做是爲了得到這樣一個切片所以if float(b[a:a+3])>=64 or float(b[a:a+3])<=254:

(在Python可以通過if 64 <= float(b[a:a+3]) <= 254:改善)

你實際上是試圖計算出這一系列的平均數?

如果是這樣,你應該這樣做:

b = list(map(lambda n: (float(n)-30)/2, b.split(",")) 
b = sum(b)/len(b) 

不與概率有關,但不是複製/粘貼一堆line=STATS11_filename.readline()你可以做[STATS11_filename.readline() for i in range(29)](儘管我認爲必須有一個函數跳到你想要的線(也許))。

編輯:事實上,你也可以這樣做:b = STATS11_filename.readlines()[29]