Jun-10-2018, 05:08 PM
Hi all!!
i am new in forum so i have very much to read!
I have one problem with some code in python. I am trying to replace None values in Json file to "vacio" values.
My example of read file is:
[{u'contributors': None,
u'coordinates': None,
u'created_at': u'Sun Jun 10 14:21:53 +0000 2018',
u'entities': {u'hashtags': [],
u'symbols': [],
u'urls': [],
u'user_mentions': [{u'id': 254599256,
u'id_str': u'254599256',
u'indices': [3, 11],
u'name': u'Alejandro Fargosi',
u'screen_name': u'fargosi'}]},
u'favorite_count': 0,
u'favorited': False,
u'filter_level': u'low',
u'geo': None,
u'id': 1005817329459097600L,
u'id_str': u'1005817329459097600',
u'in_reply_to_screen_name': None,
u'in_reply_to_status_id': None}]
Now, i use the next code for replace None for "vacio":
'[{"quote_count": 0, "contributors": vacio, "truncated": false, "text": "RT @fargosi: Del 20-4 al 10-6-18, nuestros defensores:\\nMauro Maldonado muerto\\nAbigail Alvarez muerta\\nJorge Cusi muerto\\nDaniel Rios, muerto\\u2026", "is_quote_status": false, "in_reply_to_status_id": vacio, "reply_count": 0, "id": 1005817329459097600, "favorite_count": 0, "entities": {"user_mentions": [{"id": 254599256, "indices": [3, 11], "id_str": "254599256", "screen_name": "fargosi", "name": "Alejandro Fargosi"}], "symbols": [], "hashtags": [], "urls": []}, "retweeted": false, "coordinates": vacio, "timestamp_ms": "1528640513182", "source": "<a href=\\"http://twitter.com/download/iphone\\" rel=\\"nofollow\\">Twitter for iPhone</a>", "in_reply_to_screen_name": vacio, "id_str": "1005817329459097600", "retweet_count": 0, "in_reply_to_user_id": vacio, "favorited": false, "retweeted_status":... }]
Now, i want to transform this result to a new object in format of dict for extract data more easy, such as first result. I tried
I next would execute next code, for this reason i need access by dict format:
i am new in forum so i have very much to read!
I have one problem with some code in python. I am trying to replace None values in Json file to "vacio" values.
My example of read file is:
data = [] for reng in open("con_error.json", "r"): data.append(json.loads(reng))and the result is:
[{u'contributors': None,
u'coordinates': None,
u'created_at': u'Sun Jun 10 14:21:53 +0000 2018',
u'entities': {u'hashtags': [],
u'symbols': [],
u'urls': [],
u'user_mentions': [{u'id': 254599256,
u'id_str': u'254599256',
u'indices': [3, 11],
u'name': u'Alejandro Fargosi',
u'screen_name': u'fargosi'}]},
u'favorite_count': 0,
u'favorited': False,
u'filter_level': u'low',
u'geo': None,
u'id': 1005817329459097600L,
u'id_str': u'1005817329459097600',
u'in_reply_to_screen_name': None,
u'in_reply_to_status_id': None}]
Now, i use the next code for replace None for "vacio":
json_string=json.dumps(data).replace("null", "vacio")and the result is:
'[{"quote_count": 0, "contributors": vacio, "truncated": false, "text": "RT @fargosi: Del 20-4 al 10-6-18, nuestros defensores:\\nMauro Maldonado muerto\\nAbigail Alvarez muerta\\nJorge Cusi muerto\\nDaniel Rios, muerto\\u2026", "is_quote_status": false, "in_reply_to_status_id": vacio, "reply_count": 0, "id": 1005817329459097600, "favorite_count": 0, "entities": {"user_mentions": [{"id": 254599256, "indices": [3, 11], "id_str": "254599256", "screen_name": "fargosi", "name": "Alejandro Fargosi"}], "symbols": [], "hashtags": [], "urls": []}, "retweeted": false, "coordinates": vacio, "timestamp_ms": "1528640513182", "source": "<a href=\\"http://twitter.com/download/iphone\\" rel=\\"nofollow\\">Twitter for iPhone</a>", "in_reply_to_screen_name": vacio, "id_str": "1005817329459097600", "retweet_count": 0, "in_reply_to_user_id": vacio, "favorited": false, "retweeted_status":... }]
Now, i want to transform this result to a new object in format of dict for extract data more easy, such as first result. I tried
json.loads(json_string)but i have error : No JSON object could be decoded
I next would execute next code, for this reason i need access by dict format:
usuarios = [] texto = [] fechas = [] localizaciones = [] for line in open('con_error.json', 'r'): usuarios.append(json.loads(line)['user']['name']) texto.append(json.loads(line)['text']) fechas.append(json.loads(line)['created_at']) localizaciones.append(json.loads(line)['place']['name'])Thank you very much and sorry if there is some mystake in my post. It is my first post!