python 3.x - Python3: dictionary of dictionaries for table-content in file -
In my hand, where I was trapped, There is something like this in the file: (the first six lines of ascii-file)
The name ----------- | Alt name ------- | ---- --RA | ----- Dec | ----- z | --- CR | ---- FX | --- FX * | Error | --- LX | --NH | ID- | Ref # ---- RXCJ0000.1 + 0816 UGC12890 0,0295 82,240 0,0396 0,26 5,80 5,39 12,4 0,37 5,9 1,3 RXCJ0001.9 + 1204 A2692 0, 4877 12,0730 02033 0,08 1,82 1,81 17,93,24 5,1 1 RXCJ0004.9 + 1142 UGC00032 1.2473 11.7006 0,0761 0,17 3,78 3,68 12 , 7,093 5,3, 2,4
RXCJ0005.3 + 1612 A2703 1,3440 16,2105 0,1164 0,24 4,96 4,94 11,8, 2,88 3,7 B2, 5
RXCJ0006.3 + 1052 a) 1,5906 10,8677 0,1698 0,15 3,28 3,28 19,3 4,05 5.6 1
I I can provide file samples.
This code works fine until each code is stored in other words.
to import from the archive! Import / usr / bin / env python3 file * obsrun = {} objects = {} re = compile ('\ d +. \ D \ d \ d \ d') with file name = 'test.asc' open ( File name, 'r') as f: line = f.readlines () for line [2:]: #split Reading lines in a list o_bject = l.split () # print (o_big) # each entry and Interact with people at line-dict with the value of interest # which is needed (in the table column): Identifier, common name, right piece, declines in o_bject for Kashmir: Object .__ setitem __ ('id', o_bject [0]) .__ setitem __ ('COMMON_NAME', o_bject [1]) is sometimes common name cartridge, many entries or replacements if re.match (o_bject [2] ) Objects: objects .__ setitem __ ('ra', boat .__ setitem __ ('December', boat (o_bject [3])) and (o_bject [2]) objects: objects .__ setitem __ ('ra' (O_bject [4]) key name = objects.get ('id') #print (name) print (object) # * # Remove identifier (object name) to use as http: /stackoverflow.com/questi Ons / 1024847 / Add-To-A-Dictionary-In-Perthon obsrun [ Name] = objects # print (obsrun) ordered # objects ordered by order order ordered dictionary (sorted (obsrun.items), key = lambda t: t [0])) # t [0] Keys, T [1] value
What can anyone see from the output on the console, what the internal for-loop does. This is confirmed by print (objects) * *. But in other words when it comes to collecting line-items in value, then those people with the same value are the keys that are made properly.
That which I do not understand, print () displays the correct contents of the "objects" command, but they are not correctly stored in "observation"
Thank you in advance, Christian
You only created one dictionary, so each time loop By means you are amending it.
objects = {}
for line in [2:]:
by loop Each line of the file will create a different word for it.
In addition, it is unnecessary to use the __settings __
and makes the code difficult to read. Change the lines from objects .__ setitem __ ('id', o_bject [0])
to objects ['id'] = o_bject [0]
.
Comments
Post a Comment