如何从文本fi创建值

2024-06-16 11:21:26 发布

您现在位置:Python中文网/ 问答频道 /正文

首先我要说的是,我是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块,“标签”是temperatuur(温度)、vochtigheid(湿度)、windrichting(风向)和windkracht(风速)。这些“标签”24次重复,因为它们一整天都是每小时拍一次。 赋值是只获取标签“temperatuur”(荷兰语中的温度)的值,并从中列出一个列表,并将该列表保存在一个单独的文本文件中。第一个值是以摄氏度为单位的温度,第二个值是以毫伏为单位的相关电压。你知道吗

第二个赋值是生成一个图形,读取先前创建的文本文件(因此是在第一个赋值中创建的文本文件),并从中生成一个图形。x轴是小时,y轴是温度值(摄氏度)。你知道吗

我一个人走了这么远:

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']

正如你所见,这并不多。 有人能帮我解释清楚你做了什么吗


Tags: txtthatit标签温度文本文件赋值lista
2条回答

如果要只读以“temperatuur”开头的行:

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

如果要将“temperatuur 20.8 10.4\n”这样的行拆分为三个值,请使用split函数(但不要忘记value1和value2将是字符串,如果要创建图形,则必须将它们转换为数字):

label, value1, value2 = line.split()

可以使用fileinput模块读取文本文件,并使用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

相关问题 更多 >