我有这样的文件,我想从第by walk
行一直读到第car
行,然后将它添加到字典中,其中键是时间7.00 - 8.00
,值是数字150
。你知道吗
例如
by_walk = {"7.00 - 8.00":150, "8.00 - 9.00":175 and et cetera}
car = {"7.00 - 8.00":150, "8.00 - 9.00":175 and et cetera}
bus = {"7.00 - 8.00":150, "8.00 - 9.00":175 and et cetera}
我该怎么做?你知道吗
By walk
7.00 - 8.00 - 150
8.00 - 9.00 - 175
9.00 - 10.00 - 120
10.00 - 11.00 - 30
11.00 - 12.00 - 10
12.00 - 13.00 - 10
13.00 - 14.00 - 10
14.00 - 15.00 - 10
15.00 - 16.00 - 10
16.00 - 17.00 - 175
17.00 - 18.00 - 150
18.00 - 19.00 - 50
Car
7.00 - 8.00 - 150
8.00 - 9.00 - 175
9.00 - 10.00 - 120
10.00 - 11.00 - 30
11.00 - 12.00 - 10
12.00 - 13.00 - 10
13.00 - 14.00 - 10
14.00 - 15.00 - 10
15.00 - 16.00 - 10
16.00 - 17.00 - 175
17.00 - 18.00 - 150
18.00 - 19.00 - 50
Bus
7.00 - 8.00 - 150
8.00 - 9.00 - 175
9.00 - 10.00 - 120
10.00 - 11.00 - 30
11.00 - 12.00 - 10
12.00 - 13.00 - 10
13.00 - 14.00 - 10
14.00 - 15.00 - 10
15.00 - 16.00 - 10
16.00 - 17.00 - 175
17.00 - 18.00 - 150
18.00 - 19.00 - 50
感谢所有的回答,我有一个问题,我不知道如何阅读线从汽车到公共汽车,这是我的代码:
by_walk = {}
car = {}
bus = {}
for line in open("test.txt"):
if line.strip() != "Car":
if line.strip() == "By walk":
continue
line = line.rsplit('-', 1)
by_walk[line[0].strip()] = int(line[1])
elif line.strip() == "Car":
break
for line in open("test.txt"):
但在第一次循环之后,我不知道该做什么,需要编写什么代码。你知道吗
在:
输出:
试试这个。你知道吗
变量
q
仅在第一种传输模式之前有“time/count”行的情况下出现(这可能是文件中的错误)。假设以字母开头的行是传输模式,其他行是时间/计数。可以进行调整(例如,删除注释行)。你知道吗也可以通过使用空的
tbl
来适应未知的传输模式,并且只在遇到传输模式时添加它们。你知道吗逐行读取文件并查看该行是否包含
-
。如果是这样的话,你就知道你必须从那里开始编字典。否则,将把形成的字典附加到列表中。 这项法规规定-输出:
相关问题 更多 >
编程相关推荐