2014-10-27 76 views
0

我得開始說我是python的超級初學者,這也是我在這裏的第一篇文章,所以很受建設性的批評。所以我得到了一個任務,我需要從文本文件中取出一些值並列出它們的列表,但我不知道如何執行此操作。 文本文件如下:如何從文本文件中創建值

temperatuur 20.8 10.4 
vochtigheid 70 14 
windrichting Z 60 
windkracht 6 60 
temperatuur 21.8 10.9 
vochtigheid 60 12 
windrichting Z 60 
windkracht 4 40 
temperatuur 21.8 10.9 
vochtigheid 60 12 
windrichting Z 60 
windkracht 5 50 
temperatuur 21.8 10.9 
vochtigheid 60 12 
windrichting ZZW 50 
windkracht 5 50 
temperatuur 22.0 11.0 
vochtigheid 60 12 
windrichting ZZW 50 
windkracht 5 50 
temperatuur 22.2 11.1 
vochtigheid 65 13 
windrichting ZZW 50 
windkracht 5 50 
temperatuur 22.6 11.3 
vochtigheid 70 14 
windrichting ZZW 50 
windkracht 5 50 
temperatuur 22.8 11.4 
vochtigheid 60 12 
windrichting ZZW 50 
windkracht 4 40 
temperatuur 23.0 11.5 
vochtigheid 60 12 
windrichting ZZW 50 
windkracht 4 40 
temperatuur 23.0 11.5 
vochtigheid 60 12 
windrichting ZZW 50 
windkracht 3 30 
temperatuur 24.0 12.0 
vochtigheid 60 12 
windrichting Z 60 
windkracht 3 30 
temperatuur 25.0 12.5 
vochtigheid 60 12 
windrichting Z 60 
windkracht 2 20 
temperatuur 26.0 13.0 
vochtigheid 60 12 
windrichting Z 60 
windkracht 2 20 
temperatuur 27.0 13.5 
vochtigheid 60 12 
windrichting Z 60 
windkracht 2 20 
temperatuur 27.0 13.5 
vochtigheid 60 12 
windrichting Z 60 
windkracht 2 20 
temperatuur 25.0 12.5 
vochtigheid 60 12 
windrichting Z 60 
windkracht 3 30 
temperatuur 21.0 10.5 
vochtigheid 75 15 
windrichting W 40 
windkracht 5 50 
temperatuur 19.0 9.5 
vochtigheid 75 15 
windrichting W 40 
windkracht 5 50 
temperatuur 18.0 9.0 
vochtigheid 75 15 
windrichting W 40 
windkracht 5 50 
temperatuur 18.0 9.0 
vochtigheid 75 15 
windrichting W 40 
windkracht 5 50 
temperatuur 17.0 8.5 
vochtigheid 80 16 
windrichting W 40 
windkracht 6 60 
temperatuur 16.5 8.25 
vochtigheid 80 16 
windrichting W 40 
windkracht 6 60 
temperatuur 14.0 7.0 
vochtigheid 80 16 
windrichting W 40 
windkracht 6 60 
temperatuur 10.0 5.0 
vochtigheid 80 16 
windrichting W 40 
windkracht 6 60 

文本文件被稱爲「weerstation.txt」。 正如你所看到的,它被分成4塊,「標籤」是溫度,溫度,溼度,風力和風速。這些「標籤」重複24次,因爲他們每小時都會被帶走一整天。 這個任務僅取得標籤「temperatuur」(荷蘭語溫度)的值,並從中列出一個列表並將該列表保存在單獨的文本文件中。第一個值是以攝氏度爲單位的溫度,第二個是以毫伏爲單位的關聯電壓。

第二項任務是創建一個讀取先前創建的文本文件(從而在第一個任務中創建的文本文件)的圖形,並從中創建一個圖形。 x軸是小時,y軸是溫度值(以centigrades)。

我已經得到了這麼遠我自己:

L=[] 
lista = [] 
listadef = [] 

with open('weerstation.txt') as f: 
     for temperatuur in f: 
      L.append(temperatuur) 
# I used the next line just to see if it went allright and then left it there in case I need it again 
#  print(L) 

a = 0 
while (a < len(L)): 
    lista = L[a] 
    listadef.append(lista) 
    lista = [] 
    a = a+4 #I knew that the "temperatuur label" repeats itself after every 4 lines so that's why i took that route 


print(listadef) 

這一點讓我以下:

['temperatuur 20.8 10.4\n', 'temperatuur 21.8 10.9\n', 'temperatuur 21.8 10.9\n', 'temperatuur 21.8 10.9\n', 'temperatuur 22.0 11.0\n', 'temperatuur 22.2 11.1\n', 'temperatuur 22.6 11.3\n', 'temperatuur 22.8 11.4\n', 'temperatuur 23.0 11.5\n', 'temperatuur 23.0 11.5\n', 'temperatuur 24.0 12.0\n', 'temperatuur 25.0 12.5\n', 'temperatuur 26.0 13.0\n', 'temperatuur 27.0 13.5\n', 'temperatuur 27.0 13.5\n', 'temperatuur 25.0 12.5\n', 'temperatuur 21.0 10.5\n', 'temperatuur 19.0 9.5\n', 'temperatuur 18.0 9.0\n', 'temperatuur 18.0 9.0\n', 'temperatuur 17.0 8.5\n', 'temperatuur 16.5 8.25\n', 'temperatuur 14.0 7.0\n', 'temperatuur 10.0 5.0\n'] 

正如你可以看到,它的並不多。 因此,誰能幫我這有一份清晰的說明你做了什麼,拜託(:

+0

請一次只問一個問題。集中精力創建一個非常明確的問題陳述。僅添加[重現問題所需的信息](http://stackoverflow.com/help/mcve)。 – lxg 2014-10-27 08:15:13

回答

0

您可以將fileinput模塊用於r閱讀文本文件並使用matplotlib模塊繪製所需圖形。更多細節可以在下面的代碼中看到。

import fileinput 

temp_values = [] 
for line in fileinput.input("weerstation.txt"): 
    print line, type(line) 
    if 'temperatuur' in line: 
     temp_values.append(line.strip().split()[1]) # split the list and just add the temperature 

with open("temp_values.txt", "w") as fp: 
    fp.write("\n".join(temp_values))     # save values to temp_values.txt and finish assignment 1 

print temp_values 
# ['10.4', '10.9', '10.9', '10.9', '11.0', '11.1', '11.3', '11.4', '11.5', '11.5', '12.0', '12.5', '13.0', '13.5', '13.5', '12.5', '10.5', '9.5', '9.0', '9.0', '8.5', '8.25', '7.0', '5.0'] 


import matplotlib.pyplot as plt      # using matplotlib to draw figure and finish assignment 2 

plt.plot([i for i in xrange(0, 24)], temp_values) 
plt.xlabel("Hours") 
plt.ylabel("Temperatures") 
plt.show() 

enter image description here

+0

我忘了說,在第一次任務中,我需要列出兩個值(度和mV值)並將其保存到文本文件中。 然後我讀了那個文本文件來製作圖。我現在明白瞭如何將兩個值都放在文本文件中,但是如何才能讀取第一個值來生成圖形? – otherwise 2014-10-27 09:29:37

+0

要讀取'temp_values.txt',您還可以對文本文件中的每一行使用fileinput.input()函數,只需拆分該行並保留第一個值即可。該過程可以簡化爲'values = [line.split()[1] for line in fileinput.input(「temp_values.txt」)]'' – Eric 2014-10-27 09:44:37

0

如果你想只讀以「temperatuur」線:

with open('weerstation.txt') as f: 
    L = [line for line in f.readlines() if line.startswith('temperatuur')] 

如果要拆分行像「temperatuur 20.8 10.4 \ N」三個值使用分割功能(但不要忘記值1和值2將是字符串,你必須將它們轉換爲數字,如果youw要創建圖):

label, value1, value2 = line.split() 
+0

好的,非常感謝您的意見! :) – otherwise 2014-10-27 09:31:22