简体   繁体   English

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

[英]How to read specific line and then add it to dictionary python

I have file like this and I want to read it from line by walk until line car and then add it to the dictionary where key will be a time 7.00 - 8.00 and value will be number 150 .我有这样的文件,我想从在线阅读by walk至线car ,然后将其添加到字典中,其中关键是时间7.00 - 8.00和价值将是数150

For example例如

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}

How can I do this?我怎样才能做到这一点?

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

Thanks for all the answers, I have an issue I do not know how to read line from car to bus, here is my code:感谢所有的答案,我有一个问题我不知道如何读取从汽车到公共汽车的线路,这是我的代码:

 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"):

But after first loop I do not know what to do and what code I need to write.但是在第一次循环之后,我不知道该做什么以及需要编写什么代码。

Try this.尝试这个。

The variable q is only here in case there are "time/count" rows before the first mode of transport (that would probably be a bug in the file).变量q仅在第一种传输模式之前存在“时间/计数”行的情况下才出现(这可能是文件中的错误)。 A row that starts with a letter is assumed to be a mode of transort, any other row is a time/count.假设以字母开头的行是一种运输方式,任何其他行都是时间/计数。 That could be adapted (remove comment lines, for instance).这可以进行调整(例如,删除注释行)。

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

It's also possible to accomodate unknown modes of transport by using an empty tbl and only adding modes of transport when they are encountered.还可以通过使用空的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

Read the file line by line and see if the line contains - .逐行读取文件并查看该行是否包含- If it does then you know that from there you have to start making the dictionary.如果是,那么您知道从那里您必须开始制作字典。 Otherwise you append the formed dictionary to the list.否则,您将形成的字典附加到列表中。 This code does that -这段代码就是这样做的——

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

Output:输出:

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

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)

OUT:出去:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM