如何读取特定行,然后将其添加到字典python中

2024-10-05 14:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的文件,我想从第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"):

但在第一次循环之后,我不知道该做什么,需要编写什么代码。你知道吗


Tags: and代码inforbylineopencar
3条回答

在:

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'}

试试这个。你知道吗

变量q仅在第一种传输模式之前有“time/count”行的情况下出现(这可能是文件中的错误)。假设以字母开头的行是传输模式,其他行是时间/计数。可以进行调整(例如,删除注释行)。你知道吗

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

逐行读取文件并查看该行是否包含-。如果是这样的话,你就知道你必须从那里开始编字典。否则,将把形成的字典附加到列表中。 这项法规规定-

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'}}]

相关问题 更多 >