Python 中的 Json 和 Pandas 未定义 NameError
NameError is not defined with Json and Pandas in Python
尝试从 JSON 文件中提取字符串后的特定行,以 JSON 格式附加数据,然后发送到 Dataframe,这是可行的,但现在不行。有人知道为什么我现在收到 NameError 吗?
z = json.loads(行)
NameError: 名称 'line' 未定义
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)
目前,您的 for
循环从 fileinput.input(directory + "/" + t +"_type.json",inplace=True)
中获取每个项目并将其放入变量 line
中。但是这个变量只在你的 for
循环中定义,所以一旦程序离开循环,它就不再定义了。
根据您希望程序的行为方式,您有几个解决方案,但它们的工作方式几乎相同,您只需选择要执行的内容和时间:
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)
或
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)
希望对您有所帮助
尝试从 JSON 文件中提取字符串后的特定行,以 JSON 格式附加数据,然后发送到 Dataframe,这是可行的,但现在不行。有人知道为什么我现在收到 NameError 吗?
z = json.loads(行) NameError: 名称 'line' 未定义
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)
目前,您的 for
循环从 fileinput.input(directory + "/" + t +"_type.json",inplace=True)
中获取每个项目并将其放入变量 line
中。但是这个变量只在你的 for
循环中定义,所以一旦程序离开循环,它就不再定义了。
根据您希望程序的行为方式,您有几个解决方案,但它们的工作方式几乎相同,您只需选择要执行的内容和时间:
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)
或
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)
希望对您有所帮助