我正在使用oauth2
库解析jira数据库,接收一个字节对象,然后使用UTF-8
进行解码,然后使用json.loads
返回生成的字符串对象作为字典。我之后尝试使用DictWriter
将其写入平面文件,但它会返回以下错误:
File "/Users/mharris/PycharmProjects/CodeRepo/jira_oauth.py", line 206, in <module>
writer.writerows(data)
File "/Users/mharris/anaconda/lib/python3.5/csv.py", line 156, in writerows
return self.writer.writerows(map(self._dict_to_list, rowdicts))
File "/Users/mharris/anaconda/lib/python3.5/csv.py", line 150, in <genexpr>
return (rowdict.get(key, self.restval) for key in self.fieldnames)
AttributeError: 'str' object has no attribute 'get'
我正在做print(type(data))
以确认它确实是字典。
以下是我的代码的摘录:
with psycopg2.connect(
host=settings.REDSHIFT_HOST,
port=settings.REDSHIFT_PORT,
database=settings.REDSHIFT_DATABASE,
user=settings.REDSHIFT_USER,
password=settings.REDSHIFT_PASS) as conn:
cur = conn.cursor()
get_cols = '''
SELECT *
FROM ods_jira.staging_jira_issues
LIMIT 10
'''
cur.execute(get_cols)
colnames = [desc[0] for desc in cur.description]
max_res = 1000
data_url = data_url_base + mk_data_endpoint(max_res, start_at)
with gzip.open('jira_data.txt.gz', 'w') as outf:
writer = DictWriter(outf,
delimiter='|',
quotechar='"',
fieldnames=colnames,
extrasaction='ignore')
for cycle in range(1):
response, content = client.request(data_url, 'GET')
if response['status'] != '200':
raise Exception('Unable to access page, code was: ',
response['status'])
str_resp = content.decode('utf-8')
print(type(str_resp))
data = json.loads(str_resp)
print(type(data))
writer.writerows(data)
start_at += 1000
答案 0 :(得分:1)
str_resp = content.decode('utf-8')
print(type(str_resp))
data = json.loads(str_resp)
print(type(data))
writer.writerow(data)