我有这样的文件,我想从第by walk
行读取到第car
行,然后将其添加到字典中,其中key将是时间7.00 - 8.00
,value将是数字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"):
但是在第一次循环后,我不知道该怎么办以及需要编写什么代码。
答案 0 :(得分:1)
逐行读取文件,并查看该行是否包含-
。如果是这样,那么您就知道必须从那里开始制作字典。否则,您将形成的字典添加到列表中。
这段代码做到了-
travel_list = []
time_dict = dict()
with open('tmp.txt', 'r') as f:
for line in f:
s = line.rsplit('-', 1)
if '-' in line:
time_dict[s[0]] = s[1].rstrip()
else:
time_dict = dict()
travel_list.append({line.rstrip(): time_dict})
输出:
Out[20]:
[{'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'}}]
答案 1 :(得分:0)
尝试一下。
仅在第一种传输方式之前存在“时间/计数”行的情况下使用变量q
(这可能是文件中的错误)。假定以字母开头的行是横行模式,其他任何行都是时间/计数。可以进行修改(例如,删除注释行)。
by_walk = {}
car = {}
bus = {}
tbl = {"By walk": by_walk, "Car": car, "Bus": bus}
q = False
with open("test.txt", "rt") as f:
for s in f:
s = s.rstrip("\r\n")
if s[0].isalpha():
q = True
h = tbl[s]
elif q:
u, v = s.rsplit("-", 1)
u = u.strip()
v = int(v)
h[u] = v
还可以通过使用空的tbl
来适应未知的运输方式,并且仅在遇到运输方式时添加运输方式。
from collections import defaultdict
tbl = defaultdict(dict)
q = False
with open("test.txt", "rt") as f:
for s in f:
s = s.rstrip("\r\n")
if s[0].isalpha():
q = True
h = tbl[s]
elif q:
u, v = s.rsplit("-", 1)
u = u.strip()
v = int(v)
h[u] = v
答案 2 :(得分:0)
IN:
import re
dict1 = dict()
readValues = iter(re.split('\n', open("file.txt", "r").read()))
next(readValues)
for v in readValues:
rV = re.split("(([0-9- ]{1,2}.[0-9- ]{1,2}) - ([0-9- ]{1,2}.[0-9- ]{1,2})\w+)", v)
dict1[rV[1]] = rV[4].replace("-", "").strip()
print(dict1)
输出:
{'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'}