I am creating a string of a dictionary (to add to a .txt file later) in Python 3.6.6.
When I hard code the string of the dictionary, I don't get any problems:
my_string = '{"source": "s3://some_s3_bucket/random_filename.csv"} \n'
print(my_string)
outputs
{"source": "s3://some_s3_bucket/random_filename.csv"}
However, when I try to substitute the hard-coded filepath for variables, it appears that Python starts assuming that "source" is a variable I want to substitute:
bucket = "some_s3_bucket"
filename = "random_filename.csv"
my_new_string = '{"source": "s3://{0}/{1}"} \n'.format(bucket, filename)
print(my_new_string)
outputs
KeyError Traceback (most recent call last) in module
--> 1 my_new_string = '{"source": "s3://{0}/{1}"} \n'.format(bucket, filename)
2 print(my_new_string)KeyError: '"source"'
How should I be formatting this string for Python to properly read my bucket and filename variables?