简体   繁体   中英

python error TypeError: not all arguments converted during string formatting

Been starring at this too long and I think I missed something dumb.. Writing a script to write information to a file. All the variables are strings being passed in from another function to write a file.

Here's my code:

 53 def makeMainTF():
 54         NameTag,mcGroupTag,mcIPTag = makeNameMCTag()
 55         yourName = getAccessSecretName()
 56         instanceType ="t2.micro"
 57         with open ("vlslabMain.tf","w") as text_file:
 58                 text_file.writelines(['provider \"aws\" {\n',
 59                                       '  ',
 60                                         'access_key = \"${var.access_key}\"\n',
 61                                       '  ',
 62                                         'secret_key = \"${var.secret_key}\"\n',
 63                                       '  ',
 64                                         'region     = \"${var.access_key}\"\n',
 65                                       '}\n\n\n',
 66                                       'resource \"aws_instance\" \"example\" {\n',
 67                                         '  ',
 68                                       'ami = \"${lookup(var.amis, var.region)}\"\n',
 69                                         '  ',
 70                                         'instance_type = \"%s\" \n}' % instanceType,
 71                                         '\n\n\n\n',
 72                                         'tags {\n',
 73                                         '   ',
 74                                         'Name = \"%s\"\n' % NameTag,
 75                                         '   ',
 76                                         'Multicast = \"%s,%s\"' % (mcGroupTag,mcIPTag),
 77                                         '   ',
 78                                         'Owner = \"%s\"' % yourName,
 79                                         '\n}'])

Not sure why I'm getting this error :

Enter Access Key: asd
Enter Secret Key: asd
Enter your name: asd
Access Key: asd
Secret Key: asd
Your full name is: asd
Traceback (most recent call last):
  File "terraTFgen.py", line 86, in <module>
    makeMainTF()
  File "terraTFgen.py", line 78, in makeMainTF
    'Owner = \"%s\"' % yourName,
TypeError: not all arguments converted during string formatting

Maybe I've been starring at it too long but I dont see the syntax mistake.

It actually wrote out

Enter Access Key: asd
Enter Secret Key: asd
Enter your name: asd
Access Key: asd
Secret Key: asd
Your full name is: asd

But the error is causing the script not to write to the actual file.

Thanks for the help! ****edit***

This is the function I used to get the yourName variable

 3 def getAccessSecretName():
  4         access_key = raw_input("Enter Access Key: ")
  5         secret_key = raw_input("Enter Secret Key: ")
  6         yourName = raw_input("Enter your name: ")
  7         print "Access Key: %s" % access_key
  8         print "Secret Key: %s" % secret_key
  9         print "Your full name is: %s" % yourName
 10         return access_key, secret_key, yourName

Replace line 78 with the following and try -

'Owner = \"%s\"' % " ".join(yourName),

Effectively, yourname seems to be a tuple.
The above code will convert it to a string value.

EDIT :- (Answer to the OP's last comment)

Look at line 10 of getAccessSecretName() function -

return access_key, secret_key, yourName

It returns a tuple of access_key, secret_key, and yourName .

So if you want only yourName to be written to your file,

(Option 1)

Replace line 55 in function getAccessSecretName() with

access_key, secret_key, yourName = getAccessSecretName()

This way you the three values are unpacked to different variables,

and replace line 78 with the following -

'Owner = \"%s\"' % yourName

EDIT 2 (Option II)

If you are only interested in yourName variable you can also do something like

yourName = getAccessSecretName()[2]

Replace line 55 with the above line. Here you will be copying the value of the tuple at a specific position to the variable yourName and ignoring other values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM