Your problem isn't actually with the print function, but with how you are iterating your for loops. I've annotated your code below, added a tip to save you some time, and included some code to get you over this hurdle. Here is a resource for for loops, and here is another resource for using lists.
Here is your code, with annotations of what's happening:
#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)
At this point in the code, you have looped through and written over missing_instances
every iteration of your loop, so you are left with only the last instance.
#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
Next, you loop through unscanned:
#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)
You don't need to join the list to convert to string
I have a script that appends to a list from a text file. I then use
''.join(mylist) to convert to type str so I can query a DynamoDB table
for the said str
For example:
If you have this list:
missing_instances = ['i-xxxxxx','i-yyyyyy','i-zzzzzz']
You can see it's datatype is list
:
>>>print(type(missing_instances))
<class 'list'>
But if you are looking at an element of that list (eg. the first element), the element's data type is str
:
>>>print(type(missing_instances[0]))
<class 'str'>
This code loops through the text file and queries each line to the database:
#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()