如何在Python中将新行追加到现有CSV文件中?

时间:2019-04-25 08:53:36

标签: python amazon-ec2 boto3

我正在尝试编写一个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()

1 个答案:

答案 0 :(得分:0)

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

<div class="grid">
  <div class="pro-features">
    <h1>CodePen PRO Features</h1>
    <p>CodePen has many PRO features including these four!</p>
  </div>
  <div class="feature-privacy">
    <h2>Privacy</h2>
    <p>You can make as many <a href="https://codepen.io/pro/privacy/">Private</a> Pens, Private Posts, and Private Collections as you wish! Private <a href="https://codepen.io/pro/projects">Projects</a> are only limited by how many total Projects your plan has.</p>
  </div>
  <div class="feature-collab">
    <h2>Collab Mode</h2>
    <p><a href="https://blog.codepen.io/documentation/pro-features/collab-mode/">Collab Mode</a> allows more than one person to edit a Pen <em>at the same time</em>.</p>
  </div>
  <div class="feature-assets">
    <h2>Asset Hosting</h2>
    <p>You'll be able to <a href="https://blog.codepen.io/documentation/pro-features/asset-hosting/">upload files</a> directly to CodePen to use in anything  you build. Drag and drop to the Asset Manager and you'll get a URL to use. Edit your text assets at any time.</p>
  </div>
  
</div>