Python 如何从文本文件创建值
首先我要说的是,我是python的超初学者,这也是我在这里的第一篇文章,因此,非常感谢对python的建设性批评。所以我得到了一个任务,我需要从一个文本文件中获取一些值,并从中列出一个列表,但我不知道如何做到这一点。 文本文件如下所示:Python 如何从文本文件创建值,python,python-3.x,Python,Python 3.x,首先我要说的是,我是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
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 temperature的值,并从中列出一个列表,并将该列表保存在单独的文本文件中。第一个值是以摄氏度为单位的温度,第二个值是以毫伏为单位的相关电压
第二个赋值是生成一个图形,读取先前创建的文本文件(即第一个赋值中创建的文本文件),并从中生成一个图形。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”开头的行:
with open('weerstation.txt') as f:
L = [line for line in f.readlines() if line.startswith('temperatuur')]
如果要将诸如“temperatuur 20.8 10.4”之类的行拆分为三个值,请使用拆分函数,但不要忘记value1和value2将是字符串,如果要创建图形,则必须将它们转换为数字:
label, value1, value2 = line.split()
您可以使用该模块读取文本文件,并使用该模块绘制所需的图形。更多细节可以在下面的代码中看到
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()
请一次只问一个问题。专注于创建一个非常清晰的问题陈述。我忘了在第一次作业中,我需要列出度数和mV值,并将其保存到一个文本文件中。然后我读了那个文本文件来绘制图表。我现在了解了如何获取这两个值并将它们放入文本文件中,但如何只读取第一个值来制作图形?要读取temp_values.txt,还可以使用fileinput.input函数,对于文本文件中的每一行,只需拆分该行并保留第一个值。该过程可以简化为values=[line.split[1]for line in fileinput.inputtemp_values.txt]
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()