迭代 for 循环列表中的两个输出字符串
Iterating for loop two output strings in list
import pytaf
def METARextraction():
with open('metars_CYMX.txt') as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
#print(content)
time_array = ["","",""] * len(content)
for values in content:
print("A")
"""
Function built to extract time from pytaf using any METARs
Extracts - 'origin_minutes',' origin_hours', 'orgin_date'
"""
timebag0 =[]
timebag1 =[]
timebag2 =[]
taf = pytaf.TAF(values)
taf_header = taf._taf_header
timebag1=taf_header['origin_minutes']
timebag2=taf_header['origin_hours'])
timebag3=taf_header['origin_date']
for k in range(0,len(content)):
time_array[k,timebag1]
time_array[k,timebag2]
time_array[k,timebag3]
return(time_array)
print(METARextraction())
metars_CYMX.txt file
CYMX 090000Z 02004KT 340V060 15SM OVC014 07/06 A3027 RMK SC8 SLP255
CYMX 090100Z 06005KT 010V080 15SM OVC010 07/06 A3026 RMK SC8 SLP250
CYMX 090200Z VRB02KT 15SM OVC008 07/06 A3024 RMK ST8 PRESFR SLP244
CYMX 090300Z VRB02KT 15SM OVC006 07/07 A3023 RMK ST8 SLP240
CYMX 090344Z 04003KT 010V090 10SM TS OVC005 07/06 A3022 RMK ST8 OCNL LTGIC E
CVCTV CLD EMBDD SLP238
CYMX 090347Z 04003KT 010V090 10SM -TSRA BKN005 OVC012CB 07/07 A3022 RMK SF6CB2 OCNL LTGIC E VIS SW-NW 5 SLP237
这个函数应该做的是每次取出来,拆分成日、时、分
因为文本文件中有 6 次迭代
我需要一个数组 time_array
['09','09','09','09','09','09']
['00','01','02','03','03','03']
['00','00','00','00','44','47']
或我正在尝试做的组合
我知道如何在 MATLAB 中使用 for 循环遍历 i 和 k
我想我可能需要使用枚举
获取错误
Traceback (most recent call last):
File "main.py", line 34, in <module>
print(METARextraction())
File "main.py", line 28, in METARextraction
time_array[k,timebag1]
TypeError: list indices must be integers or slices, not tuple
我对您的代码进行了一些更改,请在下面检查并告诉我它是否有效
import pytaf
def METARextraction():
with open('metars_CYMX.txt') as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
print(content)
hours_val = []
minutes_val =[]
date_val =[]
time_array = [date_val, minutes_val, hours_val]
for values in content:
"""
Function built to extract time from pytaf using any METARs
Extracts - 'origin_minutes',' origin_hours', 'orgin_date'
"""
taf = pytaf.TAF(values)
taf_header = taf._taf_header
hours_val.append(taf_header['origin_minutes'])
minutes_val.append(taf_header['origin_hours'])
date_val.append(taf_header['origin_date'])
return(time_array)
print(METARextraction())
输出:
[['09', '09', '09', '09', '09', '09'],
['00', '01', '02', '03', '03', '03'],
['00', '00', '00', '00', '44', '47']]
import pytaf
def METARextraction():
with open('metars_CYMX.txt') as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
#print(content)
time_array = ["","",""] * len(content)
for values in content:
print("A")
"""
Function built to extract time from pytaf using any METARs
Extracts - 'origin_minutes',' origin_hours', 'orgin_date'
"""
timebag0 =[]
timebag1 =[]
timebag2 =[]
taf = pytaf.TAF(values)
taf_header = taf._taf_header
timebag1=taf_header['origin_minutes']
timebag2=taf_header['origin_hours'])
timebag3=taf_header['origin_date']
for k in range(0,len(content)):
time_array[k,timebag1]
time_array[k,timebag2]
time_array[k,timebag3]
return(time_array)
print(METARextraction())
metars_CYMX.txt file
CYMX 090000Z 02004KT 340V060 15SM OVC014 07/06 A3027 RMK SC8 SLP255
CYMX 090100Z 06005KT 010V080 15SM OVC010 07/06 A3026 RMK SC8 SLP250
CYMX 090200Z VRB02KT 15SM OVC008 07/06 A3024 RMK ST8 PRESFR SLP244
CYMX 090300Z VRB02KT 15SM OVC006 07/07 A3023 RMK ST8 SLP240
CYMX 090344Z 04003KT 010V090 10SM TS OVC005 07/06 A3022 RMK ST8 OCNL LTGIC E
CVCTV CLD EMBDD SLP238
CYMX 090347Z 04003KT 010V090 10SM -TSRA BKN005 OVC012CB 07/07 A3022 RMK SF6CB2 OCNL LTGIC E VIS SW-NW 5 SLP237
这个函数应该做的是每次取出来,拆分成日、时、分
因为文本文件中有 6 次迭代
我需要一个数组 time_array
['09','09','09','09','09','09']
['00','01','02','03','03','03']
['00','00','00','00','44','47']
或我正在尝试做的组合
我知道如何在 MATLAB 中使用 for 循环遍历 i 和 k
我想我可能需要使用枚举
获取错误
Traceback (most recent call last):
File "main.py", line 34, in <module>
print(METARextraction())
File "main.py", line 28, in METARextraction
time_array[k,timebag1]
TypeError: list indices must be integers or slices, not tuple
我对您的代码进行了一些更改,请在下面检查并告诉我它是否有效
import pytaf
def METARextraction():
with open('metars_CYMX.txt') as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
print(content)
hours_val = []
minutes_val =[]
date_val =[]
time_array = [date_val, minutes_val, hours_val]
for values in content:
"""
Function built to extract time from pytaf using any METARs
Extracts - 'origin_minutes',' origin_hours', 'orgin_date'
"""
taf = pytaf.TAF(values)
taf_header = taf._taf_header
hours_val.append(taf_header['origin_minutes'])
minutes_val.append(taf_header['origin_hours'])
date_val.append(taf_header['origin_date'])
return(time_array)
print(METARextraction())
输出:
[['09', '09', '09', '09', '09', '09'],
['00', '01', '02', '03', '03', '03'],
['00', '00', '00', '00', '44', '47']]