gpt4 book ai didi

python - 从 .txt 文件存储到列表计划中

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:04 25 4
gpt4 key购买 nike

下面的第一部分按照我想要的方式按周分隔来实现我想要的功能,但是当我尝试将其存储在迭代 I 的计划列表中时,

这是我的代码:

schedule_file = open("sch2019.txt", "r")
schedule_list = schedule_file.readlines()
for x in schedule_list:

这是代码的输出:

week   1 : { ('LAR','CP'), ('KCC','JJ'), ('NYG','DC'), ('BB','NYJ'), ('CIN','SS'),  ('DB','OR'),  ('WR','PE'), ('GBP','CB'),  ('AF','MV'), ('PS','NEP'), ('HT','NOS'), ('IC','LAC'), ('TT','CLV'), ('SF','TBB'),  ('DL','AC'),  ('BR','MD') }

week 2 : { ('SS','PS'), ('BB','NYG'), ('DC','WR'), ('SF','CIN'), ('IC','TT'), ('JJ','HT'), ('PE','AF'), ('KCC','OR'), ('LAC','DL'), ('CB','DB'), ('AC','BR'),('NOS','LAR'), ('TBB','CP'), ('NEP','MD'), ('MV','GBP'),('CLV','NYJ') }

week 3 : {('NYJ','NEP'), ('PS','SF'), ('HT','LAC'),('NYG','TBB'), ('NOS','SS'), ('DL','PE'), ('OR','MV'), ('DB','GBP'),('LAR','CLV'), ('CB','WR'), ('CP','AC'), ('MD','DC'), ('BR','KCC'), ('CIN','BB'), ('AF','IC'), ('TT','JJ') }

week 4 : { ('TT','AF'), ('SS','AC'), ('CIN','PS'), ('NEP','BB'), ('CLV','BR'), ('OR','IC'),('TBB','LAR'), ('MV','CB'), ('CP','HT'), ('LAC','MD'), ('KCC','DL'), ('JJ','DB'), ('DC','NOS'), ('WR','NYG'), ('PE','GBP') }

我想将每周存储到一个列表时间表中,但我不知道如何实现。

我尝试做的是

schedule_file = open("sch2019.txt", "r")
schedule_list = schedule_file.readlines()
schedule = []
for x in schedule_list:
for i in x:
schedule.append(i)
print (schedule)

但它所做的只是进一步分离它。我如何将列表分成时间表的迭代?

我想要的样子:

Schedule[1]← {(LAR,CP), (KCC,JJ), (NYG,DC), (BB,NYJ), (CIN,SS), (DB,OR), (WR,PE),(GBP,CB), (AF,MV), (PS,NEP), (HT,NOS), (IC,LAC), (TT,CLV), (SF,TBB), (DL,AC),(BR,MD)}

Schedule[2]←{(SS,PS), (BB,NYG), (DC,WR), (SF,CIN), (IC,TT), (JJ,HT), (PE,AF), (KCC,OR),(LAC,DL), (CB,DB), (AC,BR), (NOS,LAR), (TBB,CP), (NEP,MD), (MV,GBP), (CLV,NYJ)}

其中 Schedule[i] 的每次迭代都包含文本文件中相应周信息的值

最佳答案

我想这就是你所追求的,工作 Repl.it here .

这会将每一行解析为固定装置列表(假设我已经正确设置了输入文本文件的格式)。然后将其存储在以周数为键的 Dict 中。

import re

with open('schedule.txt') as f:
schedule = f.readlines()

def line_parser(line):
p = re.compile("'[A-Z]{2,3}','[A-Z]{2,3}'")
fixtures = []
for match in p.findall(line):
fixtures.append(match)
return fixtures

schedule_list = {}
for week, line in enumerate(schedule):
schedule_list[week] = line_parser(line)

# Print the 10th fixture in week 3:
print(schedule_list[3][9])

# Output: 'LAC','MD'

我想你可以通过将每个灯具分割成一个存储为字典的主/客字符串来进一步分解它,但我不确定你期望什么输出格式?

import re

with open('schedule.txt') as f:
schedule = f.readlines()

def line_parser(line):
p = re.compile("[A-Z]{2,3}','[A-Z]{2,3}")
fixtures = []
for match in p.findall(line):
sides = match.split("','")
fixtures.append({
"home": sides[0],
"away": sides[1]
})
return fixtures

schedule_list = {}
for week, line in enumerate(schedule):
schedule_list[week] = line_parser(line)

# Print the 10th fixture in week 3:
print(schedule_list[3][9])

# Output: {'home': 'LAC', 'away': 'MD'}

关于python - 从 .txt 文件存储到列表计划中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58442376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com