簡體   English   中英

如何讀取特定行然后將其添加到字典python

[英]How to read specific line and then add it to dictionary 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"):

但是在第一次循環之后,我不知道該做什么以及需要編寫什么代碼。

嘗試這個。

變量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

逐行讀取文件並查看該行是否包含- 如果是,那么您知道從那里您必須開始制作字典。 否則,您將形成的字典附加到列表中。 這段代碼就是這樣做的——

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

在:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM