我有一个脚本,该脚本可以从文本文件追加到list
。然后,我使用''.join(mylist)
转换为类型str
,以便可以在DynamoDB
表中查询所说的str
。在我查询表之前,这似乎一直有效。我注意到我收到的回应是空的。在打印出每个str
之后,我注意到它们是垂直返回的。如何正确格式化字符串,以使对DynamoDB
的调用成功?
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamo = boto3.resource('dynamodb')
table = dynamo.Table('mytable')
s3.Bucket('instances').download_file('MissingInstances.txt')
with open('MissingInstances.txt', 'r') as f:
for line in f:
missing_instances = []
missing_instances.append(line)
unscanned = ''.join(missing_instances)
for i in unscanned:
print(i)
response = table.query(KeyConditionExpression=Key('EC2').eq(i))
items = response['Items']
print(items)
Contents of MissingInstances.txt:
i-xxxxxx
i-yyyyyy
i-zzzzzz
etc etc
Output of print(i)
:
i
-
x
x
x
x
x
i
-
y
y
y
y
y
etc etc
print(items)
的输出:
[]
[]
[]
etc etc
所需的输出:
i-xxxxxx
i-yyyyyy
etc etc
答案 0 :(得分:1)
您的问题实际上不是打印功能,而是迭代for循环的方式。我在下面注释了您的代码,添加了一些提示以节省您的时间,并提供了一些代码来帮助您克服这一难题。 Here是for循环的资源,而here是使用列表的另一资源。
这是您的代码,其中包含发生的情况的注释:
#import libraries, prepare the data
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamo = boto3.resource('dynamodb')
table = dynamo.Table('mytable')
s3.Bucket('instances').download_file('MissingInstances.txt')
#Opens the text file that has the name of an instance and a newline character per line
with open('MissingInstances.txt', 'r') as f:
#For each line in the text file
for line in f:
#(For each line) Create an empty list called missing_instances
missing_instances = []
#Append this line to the empty list
missing_instances.append(line)
#Put all the current values of the list into a space-delimited string
#(There is only one value because you have been overwriting the list every loop)
unscanned = ''.join(missing_instances)
在代码的这一点上,您已遍历循环并在missing_instances
上进行了循环的每次迭代写入,因此只剩下最后一个实例。
#This should print the whole list of missing_instances
>>>print(*missing_instances)
i-cccccc
#This should print the whole unscanned string
>>>print(unscanned)
i-cccccc
接下来,您循环浏览未扫描的
#For each letter in the string unscanned
for i in unscanned:
#Print the letter
print(i)
#Query using the letter (The rest of this won't work for obvious reasons)
response = table.query(KeyConditionExpression=Key('EC2').eq(i))
items = response['Items']
print(items)
您无需加入列表即可转换为字符串
我有一个脚本,该脚本会附加到文本文件的列表中。然后我用 ''.join(mylist)转换为类型str,因此我可以查询DynamoDB表 对于所说的str
例如:
如果您有此列表:
missing_instances = ['i-xxxxxx','i-yyyyyy','i-zzzzzz']
您可以看到其数据类型为list
:
>>>print(type(missing_instances))
<class 'list'>
但是,如果您正在查看该列表的某个元素(例如第一个元素),则该元素的数据类型为str
:
>>>print(type(missing_instances[0]))
<class 'str'>
此代码循环遍历文本文件并查询数据库的每一行:
#import libraries, prepare the data
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamo = boto3.resource('dynamodb')
table = dynamo.Table('mytable')
s3.Bucket('instances').download_file('MissingInstances.txt')
#Open the text file
with open('MissingInstances.txt', 'r') as f:
#Create a new list
missing_instances = []
#Loop through lines in the text file
for line in f:
#Append each line to the missing_instances list, removing the newlines
missing_instances.append(line.rstrip())
#CHECKS
#Print the whole list of missing_instances, each element on a new line
print(*missing_instances, sep='\n')
#Print the data type of missing_instances
print(type(missing_instances))
#Print the data type of the first element of missing_instances
print(type(missing_instances[0]))
#Loop through the list missing_instances
#For each string element of missing_instances
for i in missing_instances:
#Print the element
print(i)
#Query the element
response = table.query(KeyConditionExpression=Key('EC2').eq(i))
#Save the response
items = response['Items']
#Print the response
print(items)
#For good measure, close the text file
f.close()
答案 1 :(得分:0)
在将换行符附加到列表之前,请尝试剥离它们。
例如:
missing_instances.append(line.rstrip())
答案 2 :(得分:0)
Print在每个呼叫中自动引入一个新行。它不能像Java的System.out#print(String)
那样工作。例如,当我运行它时,我得到了:
for c in 'adf':
print(c)
a
d
f
这是因为在python(出于某种原因)中,字符串是可迭代的。
我不确定您的代码实际上试图做什么。我不熟悉这个Boto3库。但是,假设i-xxxxx
部分被分解为i
和xxxxx
,我将其称为id
和other_stuff
。然后,
for the_id in ids:
print(f'{the_id}-{ids}')