I'm trying to download and process the .csv files and stuck on one thing, below function to process the .csv works perfect.
def insertTimeStampcsv():
rows = []
with open(r'output.csv', 'w', newline='') as out_file:
timestamp = datetime.now()
df = pd.read_csv(getCsv())
if 'Name' in df.columns:
df.rename(columns = {"Name": "team", "Total": "cost"}, inplace = True)
df.insert(0, 'date',timestamp)
df.insert(1, 'resource_type', "pod")
df.insert(2, 'resource_name', "kubernetes")
df.insert(3, 'cluster_name', "talabat-qa-eks-cluster")
df.drop(["CPU", "GPU", "RAM", "PV", "Network", "LoadBalancer", "External", "Shared", "Efficiency"], axis=1, inplace=True)
df["team"] = df["team"].replace(["search-discovery"], "vendor-list")
df.to_csv(out_file, index=False)
return df
insertTimeStampcsv()
But when it comes to provide the .csv to above function, I'm using another function to generate the .csv using below code but it does not works… any help would be appreciated?
from datetime import datetime
import pandas as pd
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
params = {
'window': 'today',
'aggregate': 'namespace',
'accumulate': 'false',
'shareTenancyCosts': 'true',
'shareNamespaces': 'kube-system,lens-metrics,istio-system,default,newrelic,webhook,cert-manager',
'shareIdle': 'true',
'format': 'csv',
}
# temp_file_name = 'input.csv'
def getCsv():
# result = []
r = requests.get('http://localhost:9090/model/allocation', headers=headers, params=params)
lines = r.content
print(lines)
getCsv()
CodePudding user response:
getCsv()
should rather return a dataframe then you can call it in insertTimeStampcsv()
. You don't need to open any file in insertTimeStampcsv()
since getCsv()
is returning the df
.
This should allow you to process the CSV file received from the requests.get
call and save the modified dataframe to the "output.csv"
file.
Example:
def getCsv():
r = requests.get('http://localhost:9090/model/allocation', headers=headers, params=params)
with open("input.csv", "w") as f:
f.write(r.content)
df = pd.read_csv("input.csv")
return df
def insertTimeStampcsv():
df = getCsv()
if 'Name' in df.columns:
df.rename(columns = {"Name": "team", "Total": "cost"}, inplace = True)
df.insert(0, 'date', datetime.now())
df.insert(1, 'resource_type', "pod")
df.insert(2, 'resource_name', "kubernetes")
df.insert(3, 'cluster_name', "talabat-qa-eks-cluster")
df.drop(["CPU", "GPU", "RAM", "PV", "Network", "LoadBalancer", "External", "Shared", "Efficiency"], axis=1, inplace=True)
df["team"] = df["team"].replace(["search-discovery"], "vendor-list")
df.to_csv("output.csv", index=False)
return df
insertTimeStampcsv()