Python AttributeError:str没有属性get
我正在尝试将JSON文件转换为CSV文件并接收AttributeError:“str”对象没有属性“get”错误,代码如下。有人能告诉我怎么修吗?多谢各位Python AttributeError:str没有属性get,python,string,object,attributes,Python,String,Object,Attributes,我正在尝试将JSON文件转换为CSV文件并接收AttributeError:“str”对象没有属性“get”错误,代码如下。有人能告诉我怎么修吗?多谢各位 from sys import argv from os import path from types import * import argparse import logging import json import csv def main(): parser = argparse.ArgumentParser(
from sys import argv
from os import path
from types import *
import argparse
import logging
import json
import csv
def main():
parser = argparse.ArgumentParser(
description='Convert json file to csv'
)
parser.add_argument(
'-i',
'--input_file',
dest='input_file',
default=None,
required=True,
help='Source json file (mandatory)'
)
parser.add_argument(
'-o',
'--output_file',
dest='output_file',
default=None,
required=True,
help='Destination csv file (mandatory)'
)
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
json_data = []
data = None
write_header = True
item_keys = []
with open(input_file) as json_file:
json_data = json_file.read()
try:
data = json.loads(json_data)
except Exception as e:
raise e
with open(output_file, 'wb') as csv_file:
writer = csv.writer(csv_file)
for item in data:
item_values = []
for key in item:
if write_header:
item_keys.append(key)
value = item.get(key, '')
if isinstance(value, StringTypes):
item_values.append(value.encode('utf-8'))
else:
item_values.append(value)
if write_header:
writer.writerow(item_keys)
write_header = False
writer.writerow(item_values)
if __name__ == "__main__":
main()
这是你的电话号码
以下是格式化版本:
{
"off_ball_screens": [
{
"possession_id": "20131029121_0",
"outcomes": [],
"cutter_defender": 862504243765291,
"oteam": 12,
"game_code": 2013102912,
"led_to_shot": false,
"location_x": 17.57135,
"location_y": 36.37835,
"led_to_touch": false,
"screener_def_type": "jam",
"cutter_def_type": "whip",
"screener": 698955898567260,
"cutter": 716891544080147,
"season": 2013,
"dteam": 22,
"type": "Wide Pin",
"frame": 303,
"chance_id": "20131029121_0_0",
"id": "2013102912_1_303_698955898567260_716891544080147",
"period": 1,
"direct": false,
"game_clock": 713.9400024414062,
"screener_defender": 1038700886052194
},
{
"possession_id": "20131029121_0",
"outcomes": [],
"cutter_defender": 862504243765291,
"oteam": 12,
"game_code": 2013102912,
"led_to_shot": false,
"location_x": 7.12878,
"location_y": 21.16205,
"led_to_touch": false,
"screener_def_type": "drop",
"cutter_def_type": "trail",
"screener": 842413156760236,
"cutter": 716891544080147,
"season": 2013,
"dteam": 22,
"type": "Down",
"frame": 405,
"chance_id": "20131029121_0_0",
"id": "2013102912_1_405_842413156760236_716891544080147",
"period": 1,
"direct": false,
"game_clock": 709.5399780273438,
"screener_defender": 960969254745795
},
{
"possession_id": "20131029121_0",
"outcomes": [
"PASS",
"AST2"
],
"cutter_defender": 715506781586579,
"oteam": 12,
"game_code": 2013102912,
"led_to_shot": false,
"location_x": 12.18125,
"location_y": 17.70263,
"led_to_touch": true,
"screener_def_type": "jam",
"cutter_def_type": "trail",
"screener": 374399613395349,
"cutter": 42597133322477,
"season": 2013,
"dteam": 22,
"type": "Zipper",
"frame": 727,
"chance_id": "20131029121_0_1",
"id": "2013102912_1_727_374399613395349_42597133322477",
"period": 1,
"direct": true,
"game_clock": 699.0,
"screener_defender": 217592361774498
}
]
}
您的
数据
变量包含整个JSON,它只有一个键:“off\u ball\u screens”
。因此,当您使用项
对其进行迭代时,它实际上只是单个键本身。您可以使用数据[item]
访问“off\u ball\u screens”
正在索引的列表,但当您尝试迭代项目时,实际上是在迭代字符串值,而该字符串值没有get()
函数
您可以尝试以下方法:
def main():
parser = argparse.ArgumentParser(description='Convert json file to csv')
parser.add_argument(
'-i',
'--input_file',
dest='input_file',
default=None,
required=True,
help='Source json file (mandatory)'
)
parser.add_argument(
'-o',
'--output_file',
dest='output_file',
default=None,
required=True,
help='Destination csv file (mandatory)'
)
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
json_data = []
data = None
write_header = True
item_keys = []
with open(input_file) as json_file:
json_data = json_file.read()
try:
data = json.loads(json_data)
except Exception as e:
raise e
with open(output_file, 'w') as csv_file:
writer = csv.writer(csv_file)
data = data["off_ball_screens"]
for item in data:
item_values = []
for key in item:
if write_header:
item_keys.append(key)
value = item.get(key, '')
if isinstance(value, StringTypes):
item_values.append(value.encode('utf-8'))
else:
item_values.append(value)
if write_header:
writer.writerow(item_keys)
write_header = False
writer.writerow(item_values)
您的数据
变量包含整个JSON,它只有一个键:“off\u ball\u screens”
。因此,当您使用项
对其进行迭代时,它实际上只是单个键本身。您可以使用数据[item]
访问“off\u ball\u screens”
正在索引的列表,但当您尝试迭代项目时,实际上是在迭代字符串值,而该字符串值没有get()
函数
您可以尝试以下方法:
def main():
parser = argparse.ArgumentParser(description='Convert json file to csv')
parser.add_argument(
'-i',
'--input_file',
dest='input_file',
default=None,
required=True,
help='Source json file (mandatory)'
)
parser.add_argument(
'-o',
'--output_file',
dest='output_file',
default=None,
required=True,
help='Destination csv file (mandatory)'
)
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
json_data = []
data = None
write_header = True
item_keys = []
with open(input_file) as json_file:
json_data = json_file.read()
try:
data = json.loads(json_data)
except Exception as e:
raise e
with open(output_file, 'w') as csv_file:
writer = csv.writer(csv_file)
data = data["off_ball_screens"]
for item in data:
item_values = []
for key in item:
if write_header:
item_keys.append(key)
value = item.get(key, '')
if isinstance(value, StringTypes):
item_values.append(value.encode('utf-8'))
else:
item_values.append(value)
if write_header:
writer.writerow(item_keys)
write_header = False
writer.writerow(item_values)
为什么您捕获异常只是为了重新引发它?您应该始终捕获特定的异常,exception
只比裸的exception
稍差一点。你为什么不直接在文件处理程序上使用json.load
,而不是.read()
,然后使用json.load
?你能分享你的json文件中的样本行吗?你能分享一些样本json数据吗?@JkShaw我在文章末尾附上了样本。谢谢。不过,当您看到格式化的JSON时,问题应该非常明显。提示:您的内部for循环,for key in item
:迭代字符串“off\u ball\u screens”
为什么捕捉异常只是为了重新引发它?您应该始终捕获特定的异常,exception
只比裸的exception
稍差一点。你为什么不直接在文件处理程序上使用json.load
,而不是.read()
,然后使用json.load
?你能分享你的json文件中的样本行吗?你能分享一些样本json数据吗?@JkShaw我在文章末尾附上了样本。谢谢。不过,当您看到格式化的JSON时,问题应该非常明显。提示:您的内部for循环,for key in item
:迭代字符串“off\u ball\u screens”
不再有错误,但也不会写出CSV文件。你能帮忙吗?当我运行这个脚本时,它确实会生成一个CSV文件。您应该在CSV中指定您想要的内容。是否存在“未解析引用StringTypes”错误?我想这是我的问题。不,我没有错。你能告诉我哪一行有错误吗?是这一行。如果isinstance(值、StringType):。StringTypes下有一条红色的波浪线。不再有错误,但也不会写出CSV文件。你能帮忙吗?当我运行这个脚本时,它确实会生成一个CSV文件。您应该在CSV中指定您想要的内容。是否存在“未解析引用StringTypes”错误?我想这是我的问题。不,我没有错。你能告诉我哪一行有错误吗?是这一行。如果isinstance(值、StringType):。在StringTypes下面有一条红色的波浪线。