Search code examples
pythonjsonpandasdataframenameerror

NameError is not defined with Json and Pandas in Python


Trying to extract specific lines after string from a JSON file, append data in JSON format then send to to Dataframe, this was working but not now. Anyone know why I'm now getting a NameError?

z = json.loads(line) NameError: name 'line' is not defined

import fileinput, re, json
import pandas as pd

p = [
    "Test/a",
    "Test/b"
]
dir = "/home/****/"

for i,ip in enumerate(p):
    t = ip.replace('/', '')
    directory = dir + t
    found = False
    for line in fileinput.input(directory + "/" + t +"_type.json",inplace=True):
       if re.match('{"p',line):
           found = True
        if found:
           print(line,end="")

   y = {"p":"example"}

   z = json.loads(line)

   z.update(y)

   q = json.dumps(z)

   df = pd.read_json(q)
   for i, g in df.groupby([
      "Apple",
      "Bannana"
       ]):
       print(g)

Solution

  • Currently your for loop takes each item from fileinput.input(directory + "/" + t +"_type.json",inplace=True) and places it into the variable line. However this variable is only defined in your for loop so as soon as the program leaves the loop, it is not defined anymore.
    You have a couple of solutions depending on how you want your program to behave, but they all work pretty much the same, you just have to choose what you want to execute and when :

    for i,ip in enumerate(p):
        t = ip.replace('/', '')
        directory = dir + t
        found = False
        for line in fileinput.input(directory + "/" + t +"_type.json",inplace=True):
            if re.match('{"p',line):
                found = True
            if found:
                print(line,end="")
    
                # From there we add 2 levels of indentation in order to execute
                # the code if found is true
    
                y = {"p":"example"}
    
                z = json.loads(line)
    
                z.update(y)
    
                q = json.dumps(z)
    
                df = pd.read_json(q)
                for i, g in df.groupby([
                   "Apple",
                   "Bannana"
                    ]):
                    print(g)
    

    or

    for i,ip in enumerate(p):
        t = ip.replace('/', '')
        directory = dir + t
        found = False
        for line in fileinput.input(directory + "/" + t +"_type.json",inplace=True):
            if re.match('{"p',line):
                found = True
            if found:
                print(line,end="")
    
            # From there we add 1 levels of indentation in order to execute
            # the code for each iteration of the loop
    
            y = {"p":"example"}
    
            z = json.loads(line)
    
            z.update(y)
            q = json.dumps(z)
    
            df = pd.read_json(q)
            for i, g in df.groupby([
                "Apple",
                "Bannana"
                 ]):
                 print(g)
    

    I hope you will find this helpful