Output from my table.py file is as follows. My goal is to create a table in dataframe format using the information from here. (I caught the values of the tables in different formats in the pdf file). values contain information corresponding to rows or columns. The other alignment information is the information returned from the csv about their location in the pdf file.now i need to tabulate them in dataframe but i couldn't find an easy way. I need your help.
{"returnData": [{"value": "Art.", "left": 123, "top": 445, "width": 16, "height": 14, "pageNo": 1,"prob": 0.8870015740394592},{"value": "Nr.", "left": 143, "top": 445, "width": 12, "height": 14, "pageNo": 1, "prob": 0.8796614408493042}, {"value": "Beschreibung", "left": 197, "top": 446, "width": 72, "height": 14, "pageNo": 1, "prob": 0.9776557683944702}, {"value": "Kolli", "left": 501, "top": 448, "width": 25, "height": 14, "pageNo": 1, "prob": 0.8893064260482788}, {"value": "Inhalt", "left": 531, "top": 448, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9761049747467041}, {"value": "Menge", "left": 593, "top": 449, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9946377873420715},{"value": "Preis", "left": 665, "top": 449, "width": 20, "height": 14, "pageNo": 1, "prob": 0.974210262298584},{"value": "€", "left": 689, "top": 449, "width": 4, "height": 14, "pageNo": 1, "prob": 0.9806452393531799},
{"value": "Betrag", "left": 743, "top": 450, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9663920998573303},{"value": "€", "left": 778, "top": 450, "width": 5, "height": 14, "pageNo": 1, "prob": 0.9893004298210144},...]}
This should be easy enough with json_normalize.
When json_string is the string you mentioned, try:
import pandas as pd
json_string = {
"returnData":[
{"value": "Art.", "left": 123, "top": 445, "width": 16, "height": 14, "pageNo": 1,"prob": 0.8870015740394592},
{"value": "Nr.", "left": 143, "top": 445, "width": 12, "height": 14, "pageNo": 1, "prob": 0.8796614408493042},
{"value": "Beschreibung", "left": 197, "top": 446, "width": 72, "height": 14, "pageNo": 1, "prob": 0.9776557683944702},
{"value": "Kolli", "left": 501, "top": 448, "width": 25, "height": 14, "pageNo": 1, "prob": 0.8893064260482788},
{"value": "Inhalt", "left": 531, "top": 448, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9761049747467041},
{"value": "Menge", "left": 593, "top": 449, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9946377873420715},
{"value": "Preis", "left": 665, "top": 449, "width": 20, "height": 14, "pageNo": 1, "prob": 0.974210262298584},
{"value": "€", "left": 689, "top": 449, "width": 4, "height": 14, "pageNo": 1, "prob": 0.9806452393531799},
{"value": "Betrag", "left": 743, "top": 450, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9663920998573303},
{"value": "€", "left": 778, "top": 450, "width": 5, "height": 14, "pageNo": 1, "prob": 0.9893004298210144}
]
}
pd.json_normalize(json_string['returnData'])
returns:
value left top width height pageNo prob
Art. 123 445 16 14 1 0.887002
Nr. 143 445 12 14 1 0.879661
Beschreibung 197 446 72 14 1 0.977656
Kolli 501 448 25 14 1 0.889306
Inhalt 531 448 30 14 1 0.976105
Menge 593 449 30 14 1 0.994638
Preis 665 449 20 14 1 0.974210
€ 689 449 4 14 1 0.980645
Betrag 743 450 30 14 1 0.966392
€ 778 450 5 14 1 0.989300
Since these are column values, you can determine the left-rightbound of a column. f.e. all Art. Nr. values will be between ±123 and ±197, Beschreibung between ±197 and ±501. So you can map the following data according to that logic.
Hello I faced problem with json string to data.frame conversion.
print (json_resp.text)
{
"error_code": 0,
"description": "",
"img_size": { "w": 650, "h": 488 },
"people": [
{
"age": 22,
"gender": 84,
"mood": 29,
"position": { "x": 190, "y": 161, "w": 259, "h": 259 },
"rotation": { "yaw": -3, "pitch": 3, "roll": -1 },
"landmarks": { "lefteye": { "x": 371, "y": 233 }, "righteye": { "x": 266, "y": 236 }, "maskpoints": [ { "x": 371, "y": 233 }, { "x": 266, "y": 236 }, { "x": 203, "y": 234 }, { "x": 206, "y": 261 }, { "x": 212, "y": 287 }, { "x": 220, "y": 313 }, { "x": 233, "y": 338 }, { "x": 250, "y": 357 }, { "x": 273, "y": 373 }, { "x": 296, "y": 388 }, { "x": 321, "y": 394 }, { "x": 346, "y": 390 }, { "x": 371, "y": 377 }, { "x": 396, "y": 362 }, { "x": 416, "y": 341 }, { "x": 430, "y": 315 }, { "x": 437, "y": 287 }, { "x": 444, "y": 258 }, { "x": 448, "y": 227 }, { "x": 215, "y": 221 }, { "x": 226, "y": 203 }, { "x": 247, "y": 195 }, { "x": 269, "y": 198 }, { "x": 291, "y": 204 }, { "x": 336, "y": 201 }, { "x": 360, "y": 193 }, { "x": 385, "y": 190 }, { "x": 408, "y": 198 }, { "x": 423, "y": 216 }, { "x": 314, "y": 231 }, { "x": 314, "y": 247 }, { "x": 314, "y": 263 }, { "x": 314, "y": 279 }, { "x": 294, "y": 297 }, { "x": 304, "y": 300 }, { "x": 315, "y": 302 }, { "x": 327, "y": 299 }, { "x": 338, "y": 297 }, { "x": 242, "y": 236 }, { "x": 254, "y": 226 }, { "x": 271, "y": 226 }, { "x": 284, "y": 240 }, { "x": 270, "y": 245 }, { "x": 253, "y": 244 }, { "x": 349, "y": 238 }, { "x": 362, "y": 224 }, { "x": 379, "y": 224 }, { "x": 393, "y": 234 }, { "x": 381, "y": 242 }, { "x": 363, "y": 242 }, { "x": 281, "y": 332 }, { "x": 294, "y": 327 }, { "x": 306, "y": 322 }, { "x": 315, "y": 325 }, { "x": 325, "y": 323 }, { "x": 340, "y": 328 }, { "x": 357, "y": 335 }, { "x": 341, "y": 347 }, { "x": 327, "y": 354 }, { "x": 317, "y": 354 }, { "x": 306, "y": 353 }, { "x": 294, "y": 347 }, { "x": 289, "y": 333 }, { "x": 306, "y": 331 }, { "x": 316, "y": 332 }, { "x": 325, "y": 331 }, { "x": 349, "y": 334 }, { "x": 326, "y": 339 }, { "x": 316, "y": 340 }, { "x": 306, "y": 339 } ] },
"clothingcolors": [ ],
"ethnicity": { "african": 83, "asian": 0, "caucasian": 12, "hispanic": 3 },
"emotions": { "happiness": 1, "surprise": 5, "anger": 2, "disgust": 2, "fear": 1, "sadness": 11 }
}
]
However, when I try to change json string to data.frame I got:
import pandas as pd
df_json = pd.read_json(json_resp.text, typ='frame')
print (df_json)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\io\json\json.py", line 427, in read_json
result = json_reader.read()
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\io\json\json.py", line 537, in read
obj = self._get_object_parser(self.data)
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\io\json\json.py", line 556, in _get_object_parser
obj = FrameParser(json, **kwargs).parse()
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\io\json\json.py", line 652, in parse
self._parse_no_numpy()
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\io\json\json.py", line 871, in _parse_no_numpy
loads(json, precise_float=self.precise_float), dtype=None)
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\core\frame.py", line 392, in __init__
mgr = init_dict(data, index, columns, dtype=dtype)
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\core\internals\construction.py", line 212, in init_dict
return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\core\internals\construction.py", line 51, in arrays_to_mgr
index = extract_index(arrays)
File "C:\Users\uzytkownik\PycharmProjects\Face API\venv\lib\site-packages\pandas\core\internals\construction.py", line 320, in extract_index
raise ValueError('Mixing dicts with non-Series may lead to '
ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.
What shall I change in code to obtain simple data.frame?
json_normalize is what you would want to do. However, there are nested lists within which means it only normalizes/flattens out to first level.
I think the issue comes in with the landmarks.maskpoints as that creates 70 rows with 2 columns x, and y. So trying to create a single row, WITH something that contains 70 rows could be an issue.
You can sort of see what I mean if you just start trying to unwrap/flatten it little by little. Essentially to flatten, you want to normalize each part, then at the end merge them all together into a single row, but you can see what the issue is with the maskpoints.
jsonStr = '''
{
"error_code": 0,
"description": "",
"img_size": { "w": 650, "h": 488 },
"people": [
{
"age": 22,
"gender": 84,
"mood": 29,
"position": { "x": 190, "y": 161, "w": 259, "h": 259 },
"rotation": { "yaw": -3, "pitch": 3, "roll": -1 },
"landmarks": { "lefteye": { "x": 371, "y": 233 }, "righteye": { "x": 266, "y": 236 }, "maskpoints": [ { "x": 371, "y": 233 }, { "x": 266, "y": 236 }, { "x": 203, "y": 234 }, { "x": 206, "y": 261 }, { "x": 212, "y": 287 }, { "x": 220, "y": 313 }, { "x": 233, "y": 338 }, { "x": 250, "y": 357 }, { "x": 273, "y": 373 }, { "x": 296, "y": 388 }, { "x": 321, "y": 394 }, { "x": 346, "y": 390 }, { "x": 371, "y": 377 }, { "x": 396, "y": 362 }, { "x": 416, "y": 341 }, { "x": 430, "y": 315 }, { "x": 437, "y": 287 }, { "x": 444, "y": 258 }, { "x": 448, "y": 227 }, { "x": 215, "y": 221 }, { "x": 226, "y": 203 }, { "x": 247, "y": 195 }, { "x": 269, "y": 198 }, { "x": 291, "y": 204 }, { "x": 336, "y": 201 }, { "x": 360, "y": 193 }, { "x": 385, "y": 190 }, { "x": 408, "y": 198 }, { "x": 423, "y": 216 }, { "x": 314, "y": 231 }, { "x": 314, "y": 247 }, { "x": 314, "y": 263 }, { "x": 314, "y": 279 }, { "x": 294, "y": 297 }, { "x": 304, "y": 300 }, { "x": 315, "y": 302 }, { "x": 327, "y": 299 }, { "x": 338, "y": 297 }, { "x": 242, "y": 236 }, { "x": 254, "y": 226 }, { "x": 271, "y": 226 }, { "x": 284, "y": 240 }, { "x": 270, "y": 245 }, { "x": 253, "y": 244 }, { "x": 349, "y": 238 }, { "x": 362, "y": 224 }, { "x": 379, "y": 224 }, { "x": 393, "y": 234 }, { "x": 381, "y": 242 }, { "x": 363, "y": 242 }, { "x": 281, "y": 332 }, { "x": 294, "y": 327 }, { "x": 306, "y": 322 }, { "x": 315, "y": 325 }, { "x": 325, "y": 323 }, { "x": 340, "y": 328 }, { "x": 357, "y": 335 }, { "x": 341, "y": 347 }, { "x": 327, "y": 354 }, { "x": 317, "y": 354 }, { "x": 306, "y": 353 }, { "x": 294, "y": 347 }, { "x": 289, "y": 333 }, { "x": 306, "y": 331 }, { "x": 316, "y": 332 }, { "x": 325, "y": 331 }, { "x": 349, "y": 334 }, { "x": 326, "y": 339 }, { "x": 316, "y": 340 }, { "x": 306, "y": 339 } ] },
"clothingcolors": [ ],
"ethnicity": { "african": 83, "asian": 0, "caucasian": 12, "hispanic": 3 },
"emotions": { "happiness": 1, "surprise": 5, "anger": 2, "disgust": 2, "fear": 1, "sadness": 11 }
}
]
}'''
import json
from pandas.io.json import json_normalize
jsonObj = json.loads(jsonStr)
# flatten at 1st level. But still nested lists/dictionaries in column `people`
df_a = json_normalize(jsonObj)
# so flatten out people, and you'll see clothingcolors still has a list and landmarks too
df_people = json_normalize(jsonObj['people'])
df_clothingcolors = json_normalize(jsonObj['people'][0]['clothingcolors'])
df_landmarks = json_normalize(jsonObj['people'][0]['landmarks'])
# the landmarks column still need to flatten maskpoints...but maskpoints produces 70 rows, and there's your issue
df_maskpoints = json_normalize(jsonObj['people'][0]['landmarks']['maskpoints'])
So if you look at the shape of these:
print (df_a.shape)
(1, 5)
print (df_people.shape)
(1, 26)
print (df_clothingcolors.shape)
(0, 0)
print (df_landmarks.shape)
(1, 5)
print (df_maskpoints.shape)
(70, 2)
...you see the maskpoints shape is 70 rows.
BUT,
I found this blog useful. Essentially it unwraps all those nested lists so that you end up with 1 big flat table.
jsonStr = '''
{
"error_code": 0,
"description": "",
"img_size": { "w": 650, "h": 488 },
"people": [
{
"age": 22,
"gender": 84,
"mood": 29,
"position": { "x": 190, "y": 161, "w": 259, "h": 259 },
"rotation": { "yaw": -3, "pitch": 3, "roll": -1 },
"landmarks": { "lefteye": { "x": 371, "y": 233 }, "righteye": { "x": 266, "y": 236 }, "maskpoints": [ { "x": 371, "y": 233 }, { "x": 266, "y": 236 }, { "x": 203, "y": 234 }, { "x": 206, "y": 261 }, { "x": 212, "y": 287 }, { "x": 220, "y": 313 }, { "x": 233, "y": 338 }, { "x": 250, "y": 357 }, { "x": 273, "y": 373 }, { "x": 296, "y": 388 }, { "x": 321, "y": 394 }, { "x": 346, "y": 390 }, { "x": 371, "y": 377 }, { "x": 396, "y": 362 }, { "x": 416, "y": 341 }, { "x": 430, "y": 315 }, { "x": 437, "y": 287 }, { "x": 444, "y": 258 }, { "x": 448, "y": 227 }, { "x": 215, "y": 221 }, { "x": 226, "y": 203 }, { "x": 247, "y": 195 }, { "x": 269, "y": 198 }, { "x": 291, "y": 204 }, { "x": 336, "y": 201 }, { "x": 360, "y": 193 }, { "x": 385, "y": 190 }, { "x": 408, "y": 198 }, { "x": 423, "y": 216 }, { "x": 314, "y": 231 }, { "x": 314, "y": 247 }, { "x": 314, "y": 263 }, { "x": 314, "y": 279 }, { "x": 294, "y": 297 }, { "x": 304, "y": 300 }, { "x": 315, "y": 302 }, { "x": 327, "y": 299 }, { "x": 338, "y": 297 }, { "x": 242, "y": 236 }, { "x": 254, "y": 226 }, { "x": 271, "y": 226 }, { "x": 284, "y": 240 }, { "x": 270, "y": 245 }, { "x": 253, "y": 244 }, { "x": 349, "y": 238 }, { "x": 362, "y": 224 }, { "x": 379, "y": 224 }, { "x": 393, "y": 234 }, { "x": 381, "y": 242 }, { "x": 363, "y": 242 }, { "x": 281, "y": 332 }, { "x": 294, "y": 327 }, { "x": 306, "y": 322 }, { "x": 315, "y": 325 }, { "x": 325, "y": 323 }, { "x": 340, "y": 328 }, { "x": 357, "y": 335 }, { "x": 341, "y": 347 }, { "x": 327, "y": 354 }, { "x": 317, "y": 354 }, { "x": 306, "y": 353 }, { "x": 294, "y": 347 }, { "x": 289, "y": 333 }, { "x": 306, "y": 331 }, { "x": 316, "y": 332 }, { "x": 325, "y": 331 }, { "x": 349, "y": 334 }, { "x": 326, "y": 339 }, { "x": 316, "y": 340 }, { "x": 306, "y": 339 } ] },
"clothingcolors": [ ],
"ethnicity": { "african": 83, "asian": 0, "caucasian": 12, "hispanic": 3 },
"emotions": { "happiness": 1, "surprise": 5, "anger": 2, "disgust": 2, "fear": 1, "sadness": 11 }
}
]
}'''
import json
from pandas.io.json import json_normalize
def flatten_json(y):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
jsonObj = json.loads(jsonStr)
flat = flatten_json(jsonObj)
df = json_normalize(flat)
The output will be your 1 row, with 168 columns.
I have got json object after connection via Python to F.A.C.E API and analysis of one of face image.
I am newbie in python and my question is:
How to transform json object into data.frame and then into .xls/.csv?
print ('Response : ', json_resp.text)
#Console print:
Response : {
"error_code": 0,
"description": "",
"img_size": { "w": 650, "h": 488 },
"people": [
{
"age": 22,
"gender": 84,
"mood": 29,
"position": { "x": 190, "y": 161, "w": 259, "h": 259 },
"rotation": { "yaw": -3, "pitch": 3, "roll": -1 },
"landmarks": { "lefteye": { "x": 371, "y": 233 }, "righteye": { "x": 266, "y": 236 }, "maskpoints": [ { "x": 371, "y": 233 }, { "x": 266, "y": 236 }, { "x": 203, "y": 234 }, { "x": 206, "y": 261 }, { "x": 212, "y": 287 }, { "x": 220, "y": 313 }, { "x": 233, "y": 338 }, { "x": 250, "y": 357 }, { "x": 273, "y": 373 }, { "x": 296, "y": 388 }, { "x": 321, "y": 394 }, { "x": 346, "y": 390 }, { "x": 371, "y": 377 }, { "x": 396, "y": 362 }, { "x": 416, "y": 341 }, { "x": 430, "y": 315 }, { "x": 437, "y": 287 }, { "x": 444, "y": 258 }, { "x": 448, "y": 227 }, { "x": 215, "y": 221 }, { "x": 226, "y": 203 }, { "x": 247, "y": 195 }, { "x": 269, "y": 198 }, { "x": 291, "y": 204 }, { "x": 336, "y": 201 }, { "x": 360, "y": 193 }, { "x": 385, "y": 190 }, { "x": 408, "y": 198 }, { "x": 423, "y": 216 }, { "x": 314, "y": 231 }, { "x": 314, "y": 247 }, { "x": 314, "y": 263 }, { "x": 314, "y": 279 }, { "x": 294, "y": 297 }, { "x": 304, "y": 300 }, { "x": 315, "y": 302 }, { "x": 327, "y": 299 }, { "x": 338, "y": 297 }, { "x": 242, "y": 236 }, { "x": 254, "y": 226 }, { "x": 271, "y": 226 }, { "x": 284, "y": 240 }, { "x": 270, "y": 245 }, { "x": 253, "y": 244 }, { "x": 349, "y": 238 }, { "x": 362, "y": 224 }, { "x": 379, "y": 224 }, { "x": 393, "y": 234 }, { "x": 381, "y": 242 }, { "x": 363, "y": 242 }, { "x": 281, "y": 332 }, { "x": 294, "y": 327 }, { "x": 306, "y": 322 }, { "x": 315, "y": 325 }, { "x": 325, "y": 323 }, { "x": 340, "y": 328 }, { "x": 357, "y": 335 }, { "x": 341, "y": 347 }, { "x": 327, "y": 354 }, { "x": 317, "y": 354 }, { "x": 306, "y": 353 }, { "x": 294, "y": 347 }, { "x": 289, "y": 333 }, { "x": 306, "y": 331 }, { "x": 316, "y": 332 }, { "x": 325, "y": 331 }, { "x": 349, "y": 334 }, { "x": 326, "y": 339 }, { "x": 316, "y": 340 }, { "x": 306, "y": 339 } ] },
"clothingcolors": [ ],
"ethnicity": { "african": 83, "asian": 0, "caucasian": 12, "hispanic": 3 },
"emotions": { "happiness": 1, "surprise": 5, "anger": 2, "disgust": 2, "fear": 1, "sadness": 11 }
}
]
}
I would like to obtain data frame like:
error_code, age, gender, mood, (...), emotions.sadness
0 0, 0, 84, 29, (...), 11
and then
.xls or .csv in "C://Users"
You could use the methods pandas.read_json and Dataframe.to_csv:
import pandas
df = pandas.read_json(json_resp.tex, typ='frame')
df.to_csv("path_to_your_file.csv")
I'm feeding a function a string, which reads the string char by char. Based on the char being worked on, a JSON template is called from a dictionary, edited slightly and saved to a final dictionary, which will be parsed to JSON and saved.
The problem is that this template dictionary should stay constant, but it doesn't. Somehow, the values I write to the intermediate variable gets saved to the original template dictionary, messing up the subsequent data I'm trying to save.
Am I missing some basic concept of the dictionary? This is my first time working with dictionaries to such an extent so I wouldn't even be surprised.
The template dictionary:
self.map_legend = {"#": {"Id": 100, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "Wall", "PlayerNumber": 0},
"-": {"Id": 200, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "Shield", "PlayerNumber": 0},
"x": {"Id": 300, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "Alien", "PlayerNumber": 0},
"|": {"Id": 400, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "AlienBullet", "PlayerNumber": 0},
"!": {"Id": 500, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "Missile", "PlayerNumber": 0},
"i": {"Id": 500, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "Missile", "PlayerNumber": 1},
"M": {"Id": 600, "Alive": False, "X": 0, "Y": 0, "Width": 3, "Height": 1, "Type": "MissileController", "PlayerNumber": 0},
"X": {"Id": 700, "Alive": False, "X": 0, "Y": 0, "Width": 3, "Height": 1, "Type": "AlienFactory", "PlayerNumber": 0},
"A": {"Id": 800, "Alive": False, "X": 0, "Y": 0, "Width": 3, "Height": 1, "Type": "Ship", "PlayerNumber": 0},
"V": {"Id": 800, "Alive": False, "X": 0, "Y": 0, "Width": 3, "Height": 1, "Type": "Ship", "PlayerNumber": 1},
" ": {"Id": 900, "Alive": False, "X": 0, "Y": 0, "Width": 1, "Height": 1, "Type": "Space", "PlayerNumber": 0}}
The problem code:
for char in self.initial_game_map:
if char != "\n":
element = self.map_legend[char]
self.id_counters[char] += 1
element["Id"] = self.id_counters[char] + element["Id"]
element["Alive"] = True
element["X"] = char_counter % self.state_json["Map"]["Height"]
element["Y"] = char_counter / self.state_json["Map"]["Height"]
print self.map_legend[char]
print element
row.append(element)
element = {}
char_counter += 1
else:
self.state_json["Map"]["Rows"].append(row)
row = []
Some output:
V
{'Width': 3, 'PlayerNumber': 1, 'Y': 1, 'X': 2, 'Type': 'Ship', 'Id': 801, 'Alive': True, 'Height': 1}
{'Width': 3, 'PlayerNumber': 1, 'Y': 1, 'X': 2, 'Type': 'Ship', 'Id': 801, 'Alive': True, 'Height': 1}
#
{'Width': 1, 'PlayerNumber': 0, 'Y': 0, 'X': 18, 'Type': 'Wall', 'Id': 103, 'Alive': True, 'Height': 1}
{'Width': 1, 'PlayerNumber': 0, 'Y': 0, 'X': 18, 'Type': 'Wall', 'Id': 103, 'Alive': True, 'Height': 1}
the element variable is behaving as its supposed to, but you can see that self.map_legend assumes the value of element for some reason after element is changed, which is NOT what I want. What's going on?
element and self.map_legend[char] point to the same dictionary and thus if you update element you will update the values of the dictionary self.map_legend[char]. You seem to want a copy so use:
element = self.map_legend[char].copy()
Reference in the python documentation: https://docs.python.org/2/library/copy.html. As the dictionaries are shallow you don't need .deepcopy()
You need to deepcopy the dictionary, else the original will get modified.
from copy import deepcopy
element = deepcopy(self.map_legend[char])