I am trying to extract data from SAP using OData and python. I am trying to extract only 200 000 data using requests
.
The code:
# Data Chunk Settings
dataChunkSize = 50000 # Number of records per request
num_iter = 4
output_folder = "<folder_name>"
url = "{0}:{1}/sap/opu/odata/sap/{2}/{3}?$format=json".\
format(sapHostName,sapPort,odpServiceName,odpEntitySetName)
s = time.perf_counter()
try:
num_iter = num_iter
for i in range(1,dataChunkSize*num_iter,dataChunkSize):
if i == 1:
url_i = url + "&$top={0}".format(dataChunkSize) + "&$skip=0"
print(url_i)
data = requests.get(url = url_i,sapUser=sapUser,sapPassword=sapPassword)
if data.status_code == 200:
data_f = json.loads(data.text)#["d"]["results"])#.decode('utf-8').replace("'",'"')
with (os.path.join(output_folder,'file_1st.json'),'w') as j:
json.dump(data_f,j,ensure_ascii=False, indent=4)
else:
url_i = url + "&$top={0}".format(i+dataChunkSize-1) + "&$skip={0}".format(i)
print(url_i)
data = requests.get(url = url_i,sapUser=sapUser,sapPassword=sapPassword)
if data.status_code == 200:
data_f = json.loads(data.text)#["d"]["results"].decode('utf-8').replace("'",'"')
with (os.path.join(output_folder,'file_{0}.json'.format(i)),'w') as j:
json.dump(data_f,j,ensure_ascii=False, indent=4)
except Exception as e:
print(str(e))
elapsed = time.perf_counter() - s
print(f"Execution time: {elapsed:0.2f} seconds.")
When I run the above code, I get the below error:
'tuple' object does not support the context manager protocol
Not sure why I am getting this. What am I missing? Is the error related to json.loads()
part?