0
我有一個文本文件(traders.txt)讀取從段落嵌套日文N3 N4 N5的Python 3
Trade 14: 10000.0 (10000.0) of EUR_USD @ 1.07139
Trade 15: 10000.0 (10000.0) of AUD_USD @ 0.76
================================================
Trade ID = 14
Instrument = EUR_USD
Fill Price = 1.07139
Open Time = 2017-01-23T13:12:00.587829255Z
State = OPEN
Initial Trade Units = 10000.0
Current Open Trade Units = 10000.0
Realized Profit/Loss = 0.0
Unrealized Profit/Loss = -205.45
Financing = -8.4385
Trade ID = 15
Instrument = AUD_USD
Fill Price = 76.00
Open Time = 2017-01-23T13:12:00.587829255Z
State = OPEN
Initial Trade Units = 10000.0
Current Open Trade Units = 10000.0
Realized Profit/Loss = 0.0
Unrealized Profit/Loss = -105.45
Financing = -4.4385
我想要做的就是創建嵌套與另一名來自每個段落創建一個字典,並嵌套字典從儀器標記
IE newdict['AUD_USD']['Trade ID'] to return 15
and newdict['EUR_USD']['State'] to return OPEN
到目前爲止,我已經下降了不需要的第幾行,但我不知道如何將dictionarys分離
myfile = open('traders.txt', 'r')
newDict = {}
for line in myfile:
if line in ['\n','\r\n']:
break
for line in myfile:
with open("tradersddd.txt", "a") as fout:
fout.writelines(line)
listedline = line.strip().split('=') # split around the = sign
if len(listedline) > 1:
newDict[listedline[0].strip()] = listedline[1].strip()
print(newDict)
好極了!完美的作品,謝謝你的評論/解釋。非常感謝。 – kastin