1

I get TypeError: not enough arguments for format string for the following code:

 print("%s,%s,%s" % time.strftime("%m/%d/%Y %H:%M:%S"), (sensor.read_temperature(), (sensor.read_humidity()))

It should print the date/time and two variables in CSV format to the console. What's wrong with my format string?

1
  • Your code has a syntax error, as posted. Too many opening ( parentheses for the number of closing parentheses. Commented Nov 21, 2015 at 22:27

1 Answer 1

3

You only give the format string one argument; the other two values are a separate argument to the print() function.

Move the opening parenthesis:

print("%s,%s,%s" % (
    time.strftime("%m/%d/%Y %H:%M:%S"),
    sensor.read_temperature(),
    sensor.read_humidity())
)

Now all values to be interpolated are in a single tuple, applied to the '...' % portion. You could just use the print() function here though, setting the sep argument to a comma:

print(
    time.strftime("%m/%d/%Y %H:%M:%S"),
    sensor.read_temperature(),
    sensor.read_humidity(),
    sep=',')
Sign up to request clarification or add additional context in comments.

Comments

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.