【问题标题】:How to append a new row to an existing CSV file in Python?如何在 Python 中将新行附加到现有的 CSV 文件?
【发布时间】:2019-05-09 11:10:06
【问题描述】:

我正在尝试编写一个 Python 脚本,该脚本将创建一个 CSV 文件,其中第一行作为来自所有账户的 AWS EC2 实例不同的标签名称。然后它将使用来自所有实例的相应标签值填充该 CSV 文件。我能够创建 CSV 文件的标题,也可以生成由值组成的行。我相信我在代码中苦苦挣扎的地方是正确附加该行。我在下面粘贴了我的完整代码。请多多指教。谢谢。

我相信,在代码中,在这个特定的地方,我犯了一些错误:

                with open(output_file, 'a', newline='') as f:
                    writer = csv.writer(f)
                    writer.writerow(row)
                    #print(row)
                    #sys.exit()
                    #pass
#!/usr/bin/env python

import boto3
import botocore
import argparse
import csv
import logging
import datetime
import click
import yaml
import os
import time
import sys

logging.basicConfig(filename='tag-export.log', filemode='a', format='%(levelname)s - %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)

targetaccount = 'allaccounts'

# GLOBAL SESSION
sts = boto3.client('sts')

def aws_session(account_id, session_name, role_name):
    """
    Function that creates the boto3 session by assuming a cross account role

    Uses boto3 to make calls to the STS API


def account_name(session):
    """
    Function that resolves the account name (alias) to make output human friendly

    Uses boto3 to make calls to the IAM API

def get_instances(filters=[]):
    reservations = {}
    try:
        reservations = ec2.describe_instances(
            Filters=filters
        )
    except botocore.exceptions.ClientError as e:
        print(e.response['Error']['Message'])

    instances = []
    for reservation in reservations.get('Reservations', []):
        for instance in reservation.get('Instances', []):
            instances.append(instance)
    return instances

@click.command()
@click.option('--config_file', required=True, prompt=True, default=lambda: str(os.getcwd()) + '/config.yml', show_default='config.yml')

def main(config_file):
    try:
        with open(config_file, 'r') as ymlfile:
            config = yaml.load(ymlfile)
        ymlfile.close()
    except Exception as e:
        logger.error('Unable to open config file: ' + str(e))
        exit    

    # globals
    if 'accounts' in config:
        accounts = config['accounts']
    if 'role_name' in config:
        role_name = config['role_name']


    tag_set = []
    for account in accounts:
        logger.info('dispatching session call for account: ' + str(account) )
        if str(account) == str(config['sourceaccount']):
            session = boto3.Session(region_name=config['region'])
        else:
            session = aws_session(account_id=str(account), session_name='cross-account-assume-role', role_name = role_name)
        if session:
            AccountName = account_name(session)
            logger.info('Working on account:  ' + AccountName)
            print('Working on gathering tags and sorting them.....Wait...:  ')
            global ec2
            ec2 = session.client('ec2', region_name='ap-southeast-2')
            output_file = "{}-tags.csv".format(targetaccount)
            instances = get_instances()

            for instance in instances:
                for tag in instance.get('Tags', []):
                    if tag.get('Key'):
                        tag_set.append(tag.get('Key'))
            tag_set = sorted(list(set(tag_set)))
        else:
            print("did not get session")
            sys.exit()            

    if tag_set:
        print ('Tag Gathering Completed! Moving to each account to get Tag Values')
        #sys.exit()

    with open(output_file, 'a', newline='') as csvfile:
        fieldnames = ['Account'] + ['InstanceId'] + tag_set
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames,extrasaction='ignore')
        writer.writeheader()

    for account in accounts:
        if str(account) == str(config['sourceaccount']):
            session = boto3.Session(region_name=config['region'])
        else:
            session = aws_session(account_id=str(account), session_name='cross-account-assume-role', role_name = role_name)

        if session:
            AccountName = account_name(session)
            logger.info('Working on account:  ' + AccountName)
            print('Working on account:  ' + AccountName + '....')
            instances = get_instances()
            for instance in instances:
                row = {}
                row['Account'] = AccountName
                row['InstanceId'] = instance.get('InstanceId')
                #print (row)
                #sys.exit()
                for tag in instance.get('Tags', []):
                    for vtag in tag_set:
                       if vtag == tag.get('Key'):
                           #print (tag.get('Key'))
                           #print ('vtag=' + vtag)
                           #sys.exit()     
                           row[tag.get('Key')] = tag.get('Value')
                           #print(row)
                with open(output_file, 'a', newline='') as f:
                    writer = csv.writer(f)
                    writer.writerow(row)
                    #print(row)
                    #sys.exit()
                    #pass
        else:
            print("did not get session")                


if __name__ == "__main__":
    main()

【问题讨论】:

  • 您能否提供一个演示您的问题的代码的最小示例?例如,只有写标题的部分,然后写一行的部分,没有所有iffor 语句。然后,我们可以尝试重现该情况以帮助您确定问题。
  • 您好约翰,感谢您的评论。实际上,我在上面提供了我遇到问题的部分的完整代码。我已经解决了这个问题,很快就会发布解决方案。谢谢。
  • 谢谢!只是为了将来参考,您更有可能通过提供仅关注您遇到的问题的最小示例来获得答案,而不是提供需要人们投入大量资金的完整代码示例是时候了解所呈现的内容了。

标签: python amazon-ec2 boto3


【解决方案1】:

我通过更改逻辑解决了这个问题。而不是一气呵成。我首先构建了标签字典,然后我从 CSV 文件构建了另一个字典,然后将其全部写在一个块中,如下所示:

           with open(output_file, 'a', newline='') as f:
                writer = csv.writer(f)
                writer.writerow(row)

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 2019-03-26
    • 2021-09-04
    • 2019-01-09
    • 1970-01-01
    • 2020-12-26
    • 2020-12-14
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多