您如何收集 python 中特定输入元素的数据?
How do you collect data for a specific input element in python?
所以我将这些数据保存在一个名为 data.txt
的文本文件中
P005 NY 18/05/2021 23 40
P011 HK 16/05/2021 23 33
P023 TK 15/05/2021 24 41
P023 TK 15/05/2021 20 30
第一列是气象站代码,第二列是气象站首字母,第三列是日期,第四列是温度,第五列是降雨量。 (请注意,这些列没有 headers)
我导入这段文字并将它们存储在五个列表中。
然后我要求用户输入气象站代码,以便我可以输出从该站收集的数据。这是我卡住的地方
具体来说,如果用户输入 P023,我的输出应该是
TK 15/05/2021 24 41
TK 15/05/2021 20 30
我需要帮助才能使我的输出正确。我的尝试如下。
infile = open("data.txt",'r')
assert isinstance(infile, object)
data = infile.read()
infile.close()
lines = data.split("\n")
Station_ID = []
Station_Name = []
Date_Time = []
Temperature = []
Rainfall = []
for line in lines:
record = line.split( )
Station_ID.append((record[0]))
Station_Name.append(record[1])
Date_Time.append(record[2])
Temperature.append(record[3])
Rainfall.append(record[4])
loop= True
while loop:
Choice = input("Press 1 to enter code")
if Choice==1:
RequestID = input("Enter valid Weather Station ID Code: ")
# it is at this point where i have no clue what i have to do
if RequestID in Station_ID:
print (index, (value1, value2, value3, value4,) in enumerate(zip(Station_Name, Date_Time, Temperature, Rainfall))
elif choice%=2:
print ("you will now exit")
我不知道你真正想要达到什么目的,但如果符合你的需要,你可以尝试以下方法:
from collections import defaultdict
with open('data.txt', 'r') as f:
data = f.readlines()
data_dict = defaultdict(list)
for line in data:
l = line.split()
data_dict[l[0]].append(' '.join(l[1:]))
search_info = input("Provide station ID: ")
for station_ID, infos in data_dict.items():
if station_ID == search_info:
print(infos)
输出:
Provide station ID: P023
['TK 15/05/2021 24 41', 'TK 15/05/2021 20 30']
我建议您使用集合模块中的 defaultdict,其中默认为列表:
from collections import defaultdict
data_dict = defaultdict(list)
并将数据存储在那里:
infile = open("data.txt",'r')
assert isinstance(infile, object)
data = infile.read()
infile.close()
lines = data.split("\n")
for line in lines:
record = line.split( )
station, rest = record[0], record[1:]
data_dict[station].append(rest)
所以你最终得到了这样的结构 data_dict
:
defaultdict(list,
{'P005': [['NY', '18/05/2021', '23', '40']],
'P011': [['HK', '16/05/2021', '23', '33']],
'P023': [['TK', '15/05/2021', '24', '41'],
['TK', '15/05/2021', '20', '30']]})
然后就可以通过关键站访问数据了:
data_dict['P023']
#=> [['TK', '15/05/2021', '24', '41'], ['TK', '15/05/2021', '20', '30']]
由于您已经将这些详细信息存储为列表,因此 Station_ID 的索引可用于查找所需的详细信息,
if RequestID in Station_ID:
indx=Station_ID.index(RequestID)
print(Station_Name[indx],Date_Time[indx],Temperature[indx],Rainfall[indx])
下面的应该可行,至少对于这种情况
import numpy as np
path = 'yourpath\data.txt'
infile = open(path,'r')
assert isinstance(infile, object)
data = infile.read()
infile.close()
lines = data.split("\n")
Station_ID = []
Station_Name = []
Date_Time = []
Temperature = []
Rainfall = []
for line in lines:
record = line.split( )
Station_ID.append((record[0]))
Station_Name.append(record[1])
Date_Time.append(record[2])
Temperature.append(record[3])
Rainfall.append(record[4])
result=[];
Choice = input("Press 1 to enter code: ")
if int(Choice) == 1:
RequestID = input("Enter valid Weather Station ID Code: ")
# it is at this point where i have no clue what i have to do
for i in range(len(Station_ID)):
if RequestID == Station_ID[i]:
result=np.append(result, [Station_Name[i]+' '+Date_Time[i]+' '+Temperature[i]+' '+Rainfall[i]])
print(result)
else:
print ("you will now exit")
所以我将这些数据保存在一个名为 data.txt
的文本文件中P005 NY 18/05/2021 23 40
P011 HK 16/05/2021 23 33
P023 TK 15/05/2021 24 41
P023 TK 15/05/2021 20 30
第一列是气象站代码,第二列是气象站首字母,第三列是日期,第四列是温度,第五列是降雨量。 (请注意,这些列没有 headers)
我导入这段文字并将它们存储在五个列表中。
然后我要求用户输入气象站代码,以便我可以输出从该站收集的数据。这是我卡住的地方
具体来说,如果用户输入 P023,我的输出应该是
TK 15/05/2021 24 41
TK 15/05/2021 20 30
我需要帮助才能使我的输出正确。我的尝试如下。
infile = open("data.txt",'r')
assert isinstance(infile, object)
data = infile.read()
infile.close()
lines = data.split("\n")
Station_ID = []
Station_Name = []
Date_Time = []
Temperature = []
Rainfall = []
for line in lines:
record = line.split( )
Station_ID.append((record[0]))
Station_Name.append(record[1])
Date_Time.append(record[2])
Temperature.append(record[3])
Rainfall.append(record[4])
loop= True
while loop:
Choice = input("Press 1 to enter code")
if Choice==1:
RequestID = input("Enter valid Weather Station ID Code: ")
# it is at this point where i have no clue what i have to do
if RequestID in Station_ID:
print (index, (value1, value2, value3, value4,) in enumerate(zip(Station_Name, Date_Time, Temperature, Rainfall))
elif choice%=2:
print ("you will now exit")
我不知道你真正想要达到什么目的,但如果符合你的需要,你可以尝试以下方法:
from collections import defaultdict
with open('data.txt', 'r') as f:
data = f.readlines()
data_dict = defaultdict(list)
for line in data:
l = line.split()
data_dict[l[0]].append(' '.join(l[1:]))
search_info = input("Provide station ID: ")
for station_ID, infos in data_dict.items():
if station_ID == search_info:
print(infos)
输出:
Provide station ID: P023
['TK 15/05/2021 24 41', 'TK 15/05/2021 20 30']
我建议您使用集合模块中的 defaultdict,其中默认为列表:
from collections import defaultdict
data_dict = defaultdict(list)
并将数据存储在那里:
infile = open("data.txt",'r')
assert isinstance(infile, object)
data = infile.read()
infile.close()
lines = data.split("\n")
for line in lines:
record = line.split( )
station, rest = record[0], record[1:]
data_dict[station].append(rest)
所以你最终得到了这样的结构 data_dict
:
defaultdict(list,
{'P005': [['NY', '18/05/2021', '23', '40']],
'P011': [['HK', '16/05/2021', '23', '33']],
'P023': [['TK', '15/05/2021', '24', '41'],
['TK', '15/05/2021', '20', '30']]})
然后就可以通过关键站访问数据了:
data_dict['P023']
#=> [['TK', '15/05/2021', '24', '41'], ['TK', '15/05/2021', '20', '30']]
由于您已经将这些详细信息存储为列表,因此 Station_ID 的索引可用于查找所需的详细信息,
if RequestID in Station_ID:
indx=Station_ID.index(RequestID)
print(Station_Name[indx],Date_Time[indx],Temperature[indx],Rainfall[indx])
下面的应该可行,至少对于这种情况
import numpy as np
path = 'yourpath\data.txt'
infile = open(path,'r')
assert isinstance(infile, object)
data = infile.read()
infile.close()
lines = data.split("\n")
Station_ID = []
Station_Name = []
Date_Time = []
Temperature = []
Rainfall = []
for line in lines:
record = line.split( )
Station_ID.append((record[0]))
Station_Name.append(record[1])
Date_Time.append(record[2])
Temperature.append(record[3])
Rainfall.append(record[4])
result=[];
Choice = input("Press 1 to enter code: ")
if int(Choice) == 1:
RequestID = input("Enter valid Weather Station ID Code: ")
# it is at this point where i have no clue what i have to do
for i in range(len(Station_ID)):
if RequestID == Station_ID[i]:
result=np.append(result, [Station_Name[i]+' '+Date_Time[i]+' '+Temperature[i]+' '+Rainfall[i]])
print(result)
else:
print ("you will now exit")