1

What I have tried:

import simplejson
import pandas as pd
with open('/tmp/test.json') as f:
    try:
        data = pd.DataFrame(simplejson.loads(line) for line in f)
    except Exception as e:
        pass
data.to_csv('/tmp/data.csv')

Then I got the error message:

NameError: name 'data' is not defined.  

What should I do to fix this issue?

Gavin Wong
  • 1,254
  • 1
  • 6
  • 15
NovaPoi
  • 73
  • 1
  • 7
  • 1
    scope of data variable is within the try .. except scope. to access the variable data outside try except , you need to define it outside – Rajat Mishra Dec 08 '20 at 02:30
  • Try adding `data = None` above the `with` statement. – vighnesh153 Dec 08 '20 at 02:34
  • https://stackoverflow.com/questions/25666853/how-to-make-a-variable-inside-a-try-except-block-public – Rajat Mishra Dec 08 '20 at 02:37
  • @RajatMishra--[try/except does not create a new scope](https://stackoverflow.com/questions/25666853/how-to-make-a-variable-inside-a-try-except-block-public). Issue is an error occurred in try causing data not to be created.. Simple fix is to place data.to_csv(...) within the try block (after then assignment of data). – DarrylG Dec 08 '20 at 02:37

3 Answers3

2

This means that you are successfully catching an error and going to the condition in except statement and as a result your data is never loaded and the variable data is never defined. And then when you try to save data to csv, this triggers an error.

What you should do instead is to put your to_csv statement inside try-except as well:

import simplejson
import pandas as pd
with open('/tmp/test.json') as f:
    try:
        data = pd.DataFrame(simplejson.loads(line) for line in f)
        data.to_csv('/tmp/data.csv')
    except Exception as e:
        pass

Also, it is a horrible anti-pattern in Python to do except Exception as e: pass. This will result in the code that is nigh impossible to debug because you are catching every single error that might happen and not specifically the error you expect to happen. When you do this you should either log the full error stack or print it to console to be aware of what exact error you are catching:

    try:
        data = pd.DataFrame(simplejson.loads(line) for line in f)
    except Exception as e:
        err = str(e)
        print(err)        

Or you should be catching a specific error that you expect to happen here:

    try:
        data = pd.DataFrame(simplejson.loads(line) for line in f)
    except FileNotFoundError:
        pass
NotAName
  • 3,821
  • 2
  • 29
  • 44
2

For a better explaination, here how your program is running internally,

Inside try block:

  1. First the pd.Dataframe gets instantiated

    some_memory_loc STORES pd.DataFrame(simplejson.loads(line) for line in f)

  2. Then the data variable saves the memory address and type being DataFrame:

    data = some_memory_loc AND TYPE(data) = pd.DataFrame

  3. Then Program moves out of try catch block

Now, What actually happening in your case is, during the first step, program generates an exception. Thus runs directly to except block. So the 2nd step was never executed. Thus, your python program doesn't know what data is.

I hope you have understood and thus can solve on your own.

PaxPrz
  • 1,778
  • 1
  • 13
  • 29
1

Put data.to_csv("/tmp/data.csv") inside the try statement, or add code in the except statement to create the data object some other way if an exception is thrown on the data line (and it does not create the data object).

Timus
  • 10,974
  • 5
  • 14
  • 28
user14665310
  • 524
  • 4
  • 13