如何将config.json文件中的信息访问到python文件?
问题是我无法从config.json文件访问python文件中的信息 我在下面提供了JSON数据和python代码 我已经尝试了请求模块中的所有内容 但是我可以在没有配置文件的情况下访问响应,但是, 我需要配置文件 下面是一个json文件如何将config.json文件中的信息访问到python文件?,python,json,python-3.x,class,python-requests,Python,Json,Python 3.x,Class,Python Requests,问题是我无法从config.json文件访问python文件中的信息 我在下面提供了JSON数据和python代码 我已经尝试了请求模块中的所有内容 但是我可以在没有配置文件的情况下访问响应,但是, 我需要配置文件 下面是一个json文件 { "api_data": { "request_url": "https://newapi.zivame.com/api/v1/catalog/list", "post_data" : {"category_ids"
{
"api_data": {
"request_url": "https://newapi.zivame.com/api/v1/catalog/list",
"post_data" : {"category_ids" : "948",
"limit" : "10000"},
"my_headers":{"Content-Type": "application/json"}
},
"redshift":{
"host":"XXX.XXXX.XXX",
"user":"XXXX",
"password":"XXXXXXXX",
"port": 8080,
"db":"XXXX"
},
"s3":{
"access_key":"XXXXXXXXX",
"secret_key":"XXXXXXXXXX",
"region":"XX-XXXXX-1",
"path":"XXXXXXXXXXXX/XXX",
"table":"XXXXXX",
"bucket":"XXXX",
"file": "XXXXXX",
"copy_column": "XXX",
"local_path": "XXXXX"
},
"csv_file": {
"promo_zivame": ""
}
}
这就是节目
#!/usr/bin/python
import json
import psycopg2
import requests
import os
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
with open(BASE_PATH+'/config.json') as json_data_file:
data = json.load(json_data_file)
#api_config = data['api_data']
#redshift = data['redshift']
s3_config = data['s3']
#x = print(api_config.get('request_url'))
class ApiResponse:
#api response
def api_data(self, api_config):
print("starting api_data")
try:
self.ApiResponse = requests.post(api_config['request_url'], api_config['post_data'], api_config['my_headers'])
data_1 = self.ApiResponse
#data = json.dump(self.ApiResponse)
print("API Result Response")
print(())
print(self.ApiResponse)
return (self.ApiResponse)
except Exception:
print("response not found")
return False
def redshift_connect(self, redshift):
try:
# Amazon Redshift connect string
self.con = psycopg2.connect(
host=redshift['host'],
user=redshift['user'],
port=redshift['port'],
password=redshift['password'],
dbname=redshift['db'])
print(self.con)
return self.con
except Exception:
print("Error in Redshift connection")
return False
def main():
c1 = ApiResponse()
api_config = data['api_data']
redshift = data['redshift']
c1.api_data(api_config)
c1.api_data(data)
c1.redshift_connect(redshift)
if __name__=='__main__':
main()
requests.post()的第三个参数是json
。要提供标题
,您需要使用@JustinEzequiel建议的参数名。请参见此处的请求文档:2.python requests.org/en/v1.1.0/user/quickstart/#custom headers
requests.post(api_config['request_url'], json=api_config['post_data'], headers=api_config['my_headers'])
借用代码
将响应保存到一个文件('the_response.json'
)中,然后您可以传递。请注意传递给请求的stream=True
。。。但是什么是“c1=data()”?嗨@MikeGuelfi哦,对不起,这是一个错误,它的c1=ApiResponse()嗨@MikeGuelfi,即使在改变了它的工作方式之后,它仍然不工作。有错误消息吗?到底是什么不起作用<代码>数据
是否为空?请提供更多信息尝试requests.post(api_-config['request_-url'],json=api_-config['post_-data'],headers=api_-config['my_-headers'])
。注意关键字json
和headers
+1是的,先生,我已经看到了,但是由于数据量很大,我需要时间,可以将响应json存储在VARABLE中,然后传递吗?嗯,我不确定。你是说api\u config['post\u data']
太大,导致超时吗?你最好把这个问题作为一个单独的新问题问好的,先生,我还有一个问题,你能帮忙吗?喜欢它的基本,但我学习像我已经这样,通过另一个程序,我会问一个问题,现在请做审查,并做必要的请我无法张贴一个问题,你可以提供给我你的链接或东西,以便我可以发短信给你请!!谢谢您,先生,@JustinEzequiel您知道如何将变量从类函数的in-try块变量访问到另一个文件中吗?我之所以使用json文件是因为我需要它是安全的,所以有一个后续程序
import requests
api_config = {
"request_url": "https://newapi.zivame.com/api/v1/catalog/list",
"post_data" : {"category_ids" : "948", "limit" : "10000"},
"my_headers":{"Content-Type": "application/json"}
}
local_filename = 'the_response.json'
with requests.post(api_config['request_url'], json=api_config['post_data'], headers=api_config['my_headers'], stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk: # filter out keep-alive new chunks
f.write(chunk)