2017-02-26 3 views
0

Ich habe eine Textdatei (traders.txt), dieNested Dicts von den Absätzen Python 3

liest
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 

Was Ich mag würde ein Wörterbuch mit dem anderen aus jedem Absatz erstellt verschachtelt tun erstellen ist, und das verschachtelte Wörterbuch um aus dem Instrument

IE newdict['AUD_USD']['Trade ID'] to return 15 
    and newdict['EUR_USD']['State'] to return OPEN 

ich die ersten paar Zeilen fallen gelassen wurden bisher markiert werden, die nicht benötigt werden, aber ich bin nicht sicher, wie die Dictionarys trennen

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) 

Antwort

0

Folgendes sollte erreichen, was Sie wollen.

from itertools import islice 
from pprint import pprint 

res_dct = {} 
with open('traders.txt', 'r') as fin: 
    d = {} 
    # using islice to cleanly skip over first 4 lines 
    for line in islice(fin, 4, None): 
     line = line.strip() 
     # If line is not empty then add contents to dict 
     if line: 
      k, v = map(str.strip, line.split(' = ')) 
      d[k] = v 
     # If line is empty then add paragraph to res_dct 
     # and re-assign d to a new dict 
     else: 
      res_dct[d['Instrument']] = d 
      # If you don't want Instrument to be in the inner dictionary 
      # use the commented line below instead, 
      #res_dct[d.pop('Instrument')] = d 
      d = {} 
    # If the file ends and there is data in the dict 
    # add it to res_dict 
    else: 
     if d: 
      res_dct[d['Instrument']] = d 
      #res_dct[d.pop('Instrument')] = d 

pprint(res_dct) 

# Output 
{'AUD_USD': {'Current Open Trade Units': '10000.0', 
      'Fill Price': '76.00', 
      'Financing': '-4.4385', 
      'Initial Trade Units': '10000.0', 
      'Instrument': 'AUD_USD', 
      'Open Time': '2017-01-23T13:12:00.587829255Z', 
      'Realized Profit/Loss': '0.0', 
      'State': 'OPEN', 
      'Trade ID': '15', 
      'Unrealized Profit/Loss': '-105.45'}, 
'EUR_USD': {'Current Open Trade Units': '10000.0', 
      'Fill Price': '1.07139', 
      'Financing': '-8.4385', 
      'Initial Trade Units': '10000.0', 
      'Instrument': 'EUR_USD', 
      'Open Time': '2017-01-23T13:12:00.587829255Z', 
      'Realized Profit/Loss': '0.0', 
      'State': 'OPEN', 
      'Trade ID': '14', 
      'Unrealized Profit/Loss': '-205.45'}} 
+0

Excellent! funktioniert perfekt, danke auch für die Kommentare/Erklärungen. Sehr geschätzt. – kastin

Verwandte Themen