【发布时间】:2020-01-15 22:53:32
【问题描述】:
我现在正在解决如何在 csv 文件中拆分成两列的问题,看起来像这样:
我想在单独的列中分别显示标准价格和敞篷车价格。但是,它们位于一个名为“aws:offerTermOfferingClass”的属性下。您知道如何在一种类型的实例下使用可转换价格和标准价格的单独列吗?我正在尝试使用这些 ifs,但它会因错误而停止。非常感谢您提前提供的帮助!
import requests
import warnings
import pandas as pd
import numpy as np
warnings.filterwarnings('ignore')
regions=['ap-northeast-1','ap-south-1','ap-southeast-1','ap-southeast-2','eu-central-1','eu-west-1','eu-west-2','us-east-1','us-east-2','us-west-1','us-west-2']
OS=['linux','rhel','windows']
links=[]
for region in regions:
for system in OS:
links.append("https://a0.p.awsstatic.com/pricing/1.0/ec2/region/" + region + "/reserved-instance/" + system + "/index.json?")
superdict=[]
for link in links:
print("Downloading data from: " + link)
res=requests.get(link,verify=False).json()
superdict.append(res)
df={"Region":[],"System":[],"Type":[],"Standard":[],"Convertible":[],"On demand":[]}
for res in superdict:
for item in res['prices']:
if item['attributes']['aws:offerTermLeaseLength']=="3yr" \
and item['attributes']['aws:offerTermPurchaseOption']=="No Upfront":
if item['attributes']['aws:ec2:operatingSystem']=="Linux" \
and item['attributes']['aws:ec2:instanceType'].endswith('.large'):
df["Region"].append(item['attributes']['aws:region'])
df["System"].append("Linux/UNIX")
df["Type"].append(item['attributes']['aws:ec2:instanceType'])
df["On demand"].append(item['calculatedPrice']['onDemandRate']['USD'])
if item['attributes']['aws:offerTermOfferingClass'] =="standard":
df["Standard"].append(float(item['calculatedPrice']['effectiveHourlyRate']['USD']))
df["Convertible"].append(np.NaN)
elif item['attributes']['aws:offerTermOfferingClass'] =="convertible":
df["Convertible"].append(float(item['calculatedPrice']['effectiveHourlyRate']['USD']))
df["Standard"].append(np.NaN)
elif item['attributes']['aws:ec2:operatingSystem']=="RHEL":
df["Region"].append(item['attributes']['aws:region'])
df["System"].append("Red Hat Enterprise Linux")
df["Type"].append(item['attributes']['aws:ec2:instanceType'])
df["On demand"].append(item['calculatedPrice']['onDemandRate']['USD'])
if item['attributes']['aws:offerTermOfferingClass'] =="standard":
df["Standard"].append(float(item['calculatedPrice']['effectiveHourlyRate']['USD']))
df["Convertible"].append(np.NaN)
elif item['attributes']['aws:offerTermOfferingClass'] =="convertible":
df["Convertible"].append(float(item['calculatedPrice']['effectiveHourlyRate']['USD']))
df["Standard"].append(np.NaN)
elif item['attributes']['aws:ec2:operatingSystem']=="Windows":
df["Region"].append(item['attributes']['aws:region'])
df["System"].append("Windows")
df["Type"].append(item['attributes']['aws:ec2:instanceType'])
df["On demand"].append(item['calculatedPrice']['onDemandRate']['USD'])
if item['attributes']['aws:offerTermOfferingClass'] =="standard":
df["Standard"].append(float(item['calculatedPrice']['effectiveHourlyRate']['USD']))
df["Convertible"].append(np.NaN)
elif item['attributes']['aws:offerTermOfferingClass'] =="convertible":
df["Convertible"].append(float(item['calculatedPrice']['effectiveHourlyRate']['USD']))
df["Standard"].append(np.NaN)
data=pd.DataFrame.from_dict(df)
data.to_csv(r'path_to_file.csv',index=False)
【问题讨论】:
-
你能给出你得到的错误吗?
-
python ValueError: arrays must all be same length我有类似的东西。