首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jq逗号管道测定仪时os模块的语法错误

使用jq逗号管道测定仪时os模块的语法错误
EN

Stack Overflow用户
提问于 2022-10-20 11:56:05
回答 1查看 63关注 0票数 0

我在python os模块中使用aws命令,而使用jq和输出重定向,我得到了一个错误。

错误:

代码语言:javascript
复制
SyntaxError: invalid syntax

代码:

代码语言:javascript
复制
iport os
os.system('aws ce get-cost-and-usage --profile dev --time-period Start=2022-10-01,End=2022-10-31 + 1 month  - 1 second" -I) --granularity MONTHLY --metrics USAGE_QUANTITY BLENDED_COST  --group-by Type=DIMENSION,Key=SERVICE | /bin/jq  '[ .ResultsByTime[].Groups[] | select(.Metrics.BlendedCost.Amount > "0") | { (.Keys[0]): .Metrics.BlendedCost } ] | sort_by(.Amount)| add' > /tmp/cost.json')

有办法解决吗?请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-22 18:20:04

如评论部分中提到的@glenn jackman。您可以使用'''来逃避。

我正在使用下面的代码通过电子邮件发送附件和HTML视图.

代码语言:javascript
复制
#!/usr/bin/env python3
from __future__ import print_function
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
import os
import time
import io
import smtplib
import pandas as pd
from email.message import EmailMessage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#################################################################
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('expand_frame_repr', True)
#################################################################

# Create message container - the correct MIME type is msg/alternative.
mail_server = 'mail.example.nxp.com'
from_addr   = 'someEmailAddressa@example.com'
to_addr     = ', '.join(['karn.kumar@example.com'])
cc_addr     = ', '.join(['otherAddress@example.com'])
subject     = 'Devlopment Cost Report Seoul'

# Create mail body as template to be send
EMAIL_TEMPLATE = """\
<html>
  <head>
  <style>
  table, th, td {{font-size:9pt; border:1px solid black; border-collapse:collapse; text-align:left; background-color:LightGray;}}
  th, td {{padding: 5px;}}
  </style>
  </head>
  <body>
     Dear All,<br><br>

     Please Find the attached CSV file attached along with HTML view for the diffrent AW Service cost . <br><br>

     {} <br><br>
    Kind regards.<br>
    Karn
  </body>
</html>"""
# collect the data into Json and save it into a file under /tmp
os.system('''aws ce get-cost-and-usage --profile dev --time-period Start=$(date "+%Y-%m-01"),End=$(date --date="$(date +'%Y-%m-01') + 1 month  - 1 second" -I) --granularity MONTHLY --metrics USAGE_QUANTITY BLENDED_COST  --group-by Type=DIMENSION,Key=SERVICE | jq -r '[ .ResultsByTime[].Groups[] | select(.Metrics.BlendedCost.Amount > "0") | { (.Keys[0]): .Metrics.BlendedCost } ] | sort_by(.Amount)| add'  > /tmp/cost.json''')

# Once the data is created now its easy to extract using the pandas.
data = pd.read_json("/tmp/cost.json")
df = (data.T)
df = df.drop('Unit', axis=1)
df.rename(columns={'Amount': 'Amount(USD)'}, inplace=True)


def df_to_csv(df):
    with io.StringIO() as buffer:
        df.to_csv(buffer)
        return buffer.getvalue()

def send_email():
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From']    = from_addr
    msg['To']      = to_addr
    msg['Cc']      = cc_addr
    attachment = MIMEApplication(df_to_csv(df))
    attachment['Content-Disposition'] = 'attachment; filename="cost-details.csv"'
    msg.attach(attachment)
    msg.attach(MIMEText(EMAIL_TEMPLATE.format(df.to_html(index=True)), 'html'))
    server = smtplib.SMTP(mail_server)
    server.sendmail(to_addr, cc_addr, msg.as_string())
    server.quit()

if __name__ == '__main__':
    send_email()

# remove the data file from "/tmp" after data parsing.
time.sleep(30)
os.unlink("/tmp/cost.json")

结果是Blow:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74139484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档