0

say I am reading a json file.

save_list = []
json_path = '../business.json'
js = [json.loads(line) for line in open(json_path)]
for item in js:
    save_dict = {}
    try:
        save_dict['name'] = item['name']
        save_dict['neighborhood'] = item['neighborhood']
        save_dict['city'] = item['city']

The problem is in json file, some lines have no value for 'city' or 'name', which are noise data, but I don't want to remove them. I want to write a exception that can handle this none value, if there is no value, put a none to the value, the save_dict['city'] = none , same as save_dict['name'] = none. How do I write try: exception:?

HAO CHEN
  • 1,209
  • 3
  • 18
  • 32
  • You can take a look [here](https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python#24065533). – Vasilis G. Mar 25 '18 at 21:33
  • What does "some lines have no value" mean? Is the value `None`? Or is the key missing from the dict? – Aran-Fey Mar 25 '18 at 21:35

3 Answers3

2

In Python, you could write:

for item in js:
    save_dict = {}
    try:
        save_dict['name'] = item['name']
    except KeyError:
        save_dict['name'] = None
    try:
        save_dict['city'] = item['city']
    except KeyError:
        save_dict['city'] = None
    save_dict['neighborhood'] = item['neighborhood']

Note, when doing exception handling, it is good practice to wrap the smallest part of the code that could produce the exception in the try-except, and you should always try to explicitely catch an exception, i.e. use

except SomeException:

instead of a bare

except:

However, in this case, you could use the .get method, on your dict object. This will, by default, return None if the key is not present in your dictionary:

for item in js:
    save_dict = {}
    save_dict['name'] = item.get('name')
    save_dict['neighborhood'] = item['neighborhood']
    save_dict['city'] = item.get('city')

or even use a dict literal:

for item in js:
    save_dict = {'name': item.get('name')
                 'neighborhood': item['neighborhood']
                 'city': item.get('city')}

Or perhaps more succinctly, in general you can do:

fields = 'key1','key2','key3','key4','key4','key6'

for item in js:
    save_dict = {k:item.get(k) for k in fields}
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • actually there are more than 20 keys, i don't want to write try exception for each key. and some lines even don't have the keys for 'name' or 'city', not just the value of 'city' is none. – HAO CHEN Mar 25 '18 at 22:16
  • @HAOCHEN then don't. Use `.get` instead of try-except. This code specifically handles the cases *where there are no keys*, not when the values are `None`. – juanpa.arrivillaga Mar 25 '18 at 22:21
0

You could instead make use of an if in case of a none value and raise a TypeError:

if condition is None:
raise TypeError

This may solve the problem.

Fabricio
  • 97
  • 2
  • 10
0

Please try this:

save_list = []
json_path = '../business.json'
js = [json.loads(line) for line in open(json_path)]
for item in js:
    save_dict = {}
    try:
        save_dict['name'] = item['name']
    except Exception as e:
        save_dict['name'] = None
    try:
        save_dict['neighborhood'] = item['neighborhood']
    except Exception as e:
        save_dict['neighborhood'] = None
    try:
        save_dict['city'] = item['city']
    except Exception as e:
        save_dict['city'] = None
Rajnil Guha
  • 425
  • 1
  • 4
  • 15