gpt4 book ai didi

json - writer.writerow() 未写入正确的列

转载 作者:行者123 更新时间:2023-12-02 05:31:39 25 4
gpt4 key购买 nike

我有三个 DynamoDB 表。两个表具有属于应用程序一部分的实例 ID,另一个表是我的所有帐户中的所有实例和标签元数据的主表。我对两个表进行了两次扫描以获取实例 ID,然后在主表中查询标签元数据。但是,当我尝试将其写入 CSV 文件时,我希望每个发电机表的唯一输出有两个单独的标题部分。第一次迭代完成后,第二个文件写入将写入第一次迭代停止的最后一行,而不是从第二个 header 部分的顶部开始。下面是我的代码和输出示例,以使其清楚。

代码:

import boto3
import csv
import json
from boto3.dynamodb.conditions import Key, Attr

dynamo = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
s3 = boto3.resource('s3')

# Required resource and client calls
all_instances_table = dynamodb.Table('Master')
missing_response = dynamo.scan(TableName='T1')
installed_response = dynamo.scan(TableName='T2')

# Creates CSV DictWriter object and fieldnames
with open('file.csv', 'w') as csvfile:
fieldnames = ['Agent Not Installed', 'Not Installed Account', 'Not Installed Tags', 'Not Installed Environment', " ", 'Agent Installed', 'Installed Account', 'Installed Tags', 'Installed Environment']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()

# Find instances IDs from the missing table in the master table to pull tag metadata
for instances in missing_response['Items']:
instance_missing = instances['missing_instances']['S']
#print("Missing:" + instance_missing)
query_missing = all_instances_table.query(KeyConditionExpression=Key('ID').eq(instance_missing))

for item_missing in query_missing['Items']:
missing_id = item_missing['ID']
missing_account = item_missing['Account']
missing_tags = item_missing['Tags']
missing_env = item_missing['Environment']
# Write the data to the CSV file
writer.writerow({'Agent Not Installed': missing_id, 'Not Installed Account': missing_account, 'Not Installed Tags': missing_tags, 'Not Installed Environment': missing_env})

# Find instances IDs from the installed table in the master table to pull tag metadata
for instances in installed_response['Items']:
instance_installed = instances['installed_instances']['S']
#print("Installed:" + instance_installed)
query_installed = all_instances_table.query(KeyConditionExpression=Key('ID').eq(instance_installed))

for item_installed in query_installed['Items']:
installed_id = item_installed['ID']
print(installed_id)
installed_account = item_installed['Account']
installed_tags = item_installed['Tags']
installed_env = item_installed['Environment']

# Write the data to the CSV file
writer.writerow({'Agent Installed': installed_id, 'Installed Account': installed_account, 'Installed Tags': installed_tags, 'Installed Environment': installed_env})

输出:

这就是文件中的列/行的样子。 enter image description here

我需要每个标题部分的所有输出都位于同一行。

数据:

这是两个表的示例。

enter image description here enter image description here

示例输出:

以下是 for 循环打印出来并附加到列表的内容。

缺少:

i-0xxxxxx 333333333 foo@bar.com int 
i-0yyyyyy 333333333 foo1@bar.com int

已安装:

i-0zzzzzz 44444444 foo2@bar.com int
i-0aaaaaa 44444444 foo3@bar.com int

最佳答案

您希望将相关行收集到一个列表中以写入单个行,例如:

missing = [] # collection for missing_responses
installed = [] # collection for installed_responses

# Find instances IDs from the missing table in the master table to pull tag metadata
for instances in missing_response['Items']:
instance_missing = instances['missing_instances']['S']
#print("Missing:" + instance_missing)
query_missing = all_instances_table.query(KeyConditionExpression=Key('ID').eq(instance_missing))
for item_missing in query_missing['Items']:
missing_id = item_missing['ID']
missing_account = item_missing['Account']
missing_tags = item_missing['Tags']
missing_env = item_missing['Environment']
# Update first half of row with missing list
missing.append(missing_id, missing_account, missing_tags, missing_env)

# Find instances IDs from the installed table in the master table to pull tag metadata
for instances in installed_response['Items']:
instance_installed = instances['installed_instances']['S']
#print("Installed:" + instance_installed)
query_installed = all_instances_table.query(KeyConditionExpression=Key('ID').eq(instance_installed))

for item_installed in query_installed['Items']:
installed_id = item_installed['ID']
print(installed_id)
installed_account = item_installed['Account']
installed_tags = item_installed['Tags']
installed_env = item_installed['Environment']
# update second half of row by updating installed list
installed.append(installed_id, installed_account, installed_tags, installed_env)
# combine your two lists outside a loop
this_row = []
i = 0;
for m in missing:
# iterate through the first half to concatenate with the second half
this_row.append( m + installed[i] )
i = i +1

# adding an empty column after the write operation, manually, is optional
# Write the data to the CSV file
writer.writerow(this_row)

如果您安装的表和丢失的表在相关字段(例如时间戳或帐户 ID)上运行,那么这将起作用,您可以确保这些字段保持行以相同的顺序连接。数据样本对于真正回答这个问题很有用。

关于json - writer.writerow() 未写入正确的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53641113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com