2014-10-27 77 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