4

I want to write two variable to a file using Python.

Based on what is stated in this post I wrote:

f.open('out','w')
f.write("%s %s\n" %str(int("0xFF",16)) %str(int("0xAA",16))

But I get this error:

Traceback (most recent call last):
  File "process-python", line 8, in <module>
    o.write("%s %s\n" %str(int("0xFF", 16))  %str(int("0xAA", 16)))
TypeError: not enough arguments for format string
2
  • is this even syntactically correct? I would think it would have to be "%s %s\n" % (str(int("0xFF", 16)), str(int("0xAA", 16))) Commented May 29, 2013 at 19:15
  • Why are you using str(int()), anyway? "%i %i\n" % (int("0xFF", 16), int("0xAA",16)) would work just as well and, in my opinion, is a bit clearer. Also, if only hexadecimal strings are guaranteed to begin with 0x then you can use int(string, 0), as that will automatically convert properly-prefixed octal strings and handle decimal strings correctly as well. If all your strings are hex and might not be preceded by 0x then using int(string, 16) is probably how you need to go, though. Commented May 29, 2013 at 20:26

6 Answers 6

13

You are not passing enough values to %, you have two specifiers in your format string so it expects a tuple of length 2. Try this:

f.write("%s %s\n" % (int("0xFF" ,16), int("0xAA", 16)))
Sign up to request clarification or add additional context in comments.

2 Comments

@mahmood yes I have, what problem are you having?
ok thanks. I had problems with opened and closed brackets. Now it is ok
3

Better use format this way:

'{0} {1}\n'.format(int("0xFF",16), int("0xAA",16))

Also there is no need to wrap int with str.

2 Comments

yeah format is nice providing he is using python2.7 or greater
format is available in 2.6, although you must explicitly number the replacement fields (i.e., '{0} {1}\n'.format(...)).
3

Firstly, your opening the file is wrong f.open('out', 'w') should probably be:

f = open('out', 'w')

Then, for such simple formatting, you can use print, for Python 2.x, as:

print >> f, int('0xff', 16), int('0xaa', 16)

Or, for Python 3.x:

print(int('0xff', 16), int('0xaa', 16), file=f)

Otherwise, use .format:

f.write('{} {}'.format(int('0xff', 16), int('0xaa', 16)))

Comments

2

The % operator takes an object or tuple. So the correct way to write this is:

f.write("%s %s\n" % (int("0xFF", 16), int("0xAA",16)))

There are also many other ways how to format a string, documentation is your friend http://docs.python.org/2/library/string.html

Comments

1

You need to supply a tuple:

f.open('out','w')
f.write("%d %d\n" % (int("0xFF",16), int("0xAA",16)))

Comments

-1

This should probably be written as:

f.write("255 170\n")

1 Comment

Those are constant in the example. In reality they are variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.