如何将boto3响应写入CSV?

2024-06-16 08:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

Python/Boto3是新手,所以这有点让人困惑。我正在尝试使用csv.writer将AWS安全中心的调查结果写入csv,但仅限于响应中的某些项目。我可以获得写入csv的正确列和行,但是当我尝试循环写入器时,它只是重复同一行,而不是响应中的其他数据。我觉得我忽略了一些简单的事情,任何帮助都是感激的

def getSecurityHubFindings():
  hub = boto3.client('securityhub')
  findingsList = []
  for key in paginate(hub.get_findings, Filters=filters, PaginationConfig={'MaxItems': MAX_ITEMS}):
    scantype = key['Types']
    str1 = ''.join(scantype)
    port=key['ProductFields']['attributes:2/value']
    vgw=key['ProductFields']['attributes:3/value']
    scantype = key['Types']
    str1 = ''.join(scantype)
    findingAccountId = key['AwsAccountId']
    findingLastObservedAt=key['LastObservedAt']
    findingFirstObservedAt=key['FirstObservedAt']
    findingCreatedAt=key['CreatedAt']
    findingrecommendation=key['Remediation']['Recommendation']
    findingTypes=key['Types']
    InstanceId=key['Resources'][0]['Id']
    findingInstanceId=str(InstanceId)
    findingAppCode=key['Resources'][0]['Tags']['AppCode']
    findingGeneratorId=key['GeneratorId']
    findingProductArn=key['ProductArn']
    findingTitle=key['Title']
    findingsList.append(key)

    if (str1 == 'Software and Configuration Checks/AWS Security Best Practices/Network Reachability - Recognized port reachable from a Peered VPC'):
      vgw=''
      port=key['ProductFields'][ 'attributes:4/value']
      peeredvpc= key['ProductFields']['attributes:2/value']

    if (str1 == 'Software and Configuration Checks/AWS Security Best Practices/Network Reachability - Recognized port reachable from a Virtual Private Gateway'):
      peeredvpc=''
      sev = key['Severity']['Product']
      if (sev == 3):
        findingSeverity='LOW'
      elif (sev == 6):
        findingSeverity='MEDIUM'
      elif ( sev == 9):
        findingSeverity='HIGH'

    rows = [findingAccountId, findingGeneratorId, findingTitle,findingProductArn,findingSeverity,findingAppCode,findingFirstObservedAt,findingLastObservedAt,findingCreatedAt,findingrecommendation,findingTypes,port,vgw,peeredvpc,findingInstanceId]

    columns = ('Account ID', 'Generator ID', 'Title', 'Product ARN', 'Severity', 'AppCode', 'First Observed At','Last Observed At', 'Created At', 'Recommendation', 'Types', 'Port', 'VGW', 'Peered VPC', 'Instance #ID')

    with open(FILE_NAME, mode='w', newline='',) as writefile:
      writefile_writer = csv.writer(writefile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
      writefile_writer.writerow(columns)
      i = 0
      while i < MAX_ITEMS:
        writefile_writer.writerow(rows)
        i +=1

  return(findingsList)

Tags: csvkeyawsvalueportattributeswritertypes
2条回答

一般流程应为:

def getSecurityHubFindings():
    ...

    # Open output file and write header
    columns = ('Account ID', 'Generator ID', 'Title', 'Product ARN', 'Severity', 'AppCode', 'First Observed At','Last Observed At', 'Created At', 'Recommendation', 'Types', 'Port', 'VGW', 'Peered VPC', 'Instance #ID')

    with open(FILE_NAME, mode='w', newline='',) as writefile:
      writefile_writer = csv.writer(writefile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
      writefile_writer.writerow(columns)

      ## Loop through response
      for key in paginate(...):

          ...
          (get data here)
          ...

          # Write output
          row = [findingAccountId, findingGeneratorId, findingTitle,findingProductArn,findingSeverity,findingAppCode,findingFirstObservedAt,findingLastObservedAt,findingCreatedAt,findingrecommendation,findingTypes,port,vgw,peeredvpc,findingInstanceId]
          writefile_writer.writerow(row)

您每次都在for循环中使用“w”选项打开文件,该选项截断文件[1]并从头开始写入,因此每次都覆盖csv

街区

      while i < MAX_ITEMS:
        writefile_writer.writerow(rows)
        i +=1

这也似乎是错误的,它只是写入同一行(即使它被称为行)MAX_ITEMS的次数。您可能希望打开csv文件并在for循环之外写入头名称,然后为for循环的每个迭代写入一行

相关问题 更多 >