3

I know this question has been asked here but the solution does not work for me. I am using python 3.4.

I have the following formatting in my script :

print ( "\t {0:20} {1:<11} {2:<25} {3:11} {4:11} {5:>32}".format( files.name.split('/')[-1], sizeof_fmt(files.size),
                                                                          str( formatted_timestamp ), files.owner,
                                                                          files.version_id, files.etag ))

This works in python 2.7.x. But in 3.4 I get the error:

File "test.py", line 3, in file_print
      versionid, etag ))
TypeError: non-empty format string passed to object.__format__

I tried this:

print ( "\t {0:20} {1:<11} {2:<25} {3:11} {!s4:11s} {!s5:>32s}".format( files.name.split('/')[-1], sizeof_fmt(files.size),
                                                                          str( formatted_timestamp ), files.owner,
                                                                          files.version_id, files.etag ))

But I still get the same error. I even converted the versionid and etag to strings and end up getting the same error. Can someone explain this to me?

Etag look like this 9893532233caff98cd083a116b013c0b, versionid is None

3
  • Try narrowing it down. Remove parameters one by one from the format call until you don't get the error anymore. Developing an MCVE is the first step towards a solution. Commented Apr 13, 2015 at 19:44
  • Your code produces NameError: name 'files' is not defined, rather than TypeError. Without providing code that we can copy-paste-run, it gives the impression that you're not meeting us half way. Commented Apr 13, 2015 at 19:47
  • Conversions come after the field name; {!s4:11s} isn't valid, only {4!s:11s}. Commented Apr 13, 2015 at 19:52

1 Answer 1

10

One of your parameters is a type that doesn't have their own __format__() method, so object.__format__() is used instead.

object.__format__() doesn't support any formatting options, including field widths and alignment, which is why you get the error.

Conversion to string first should help, but you do need to put your conversion after the field name; instead of {!s4:11s} use {4!s:11s}, etc:

print ( "\t {0:20} {1:<11} {2:<25} {3:11} {4!s:11s} {5!s:>32s}".format(
    files.name.split('/')[-1], sizeof_fmt(files.size),                                                                           
    str(formatted_timestamp), files.owner,
    files.version_id, files.etag))
Sign up to request clarification or add additional context in comments.

2 Comments

This works. I could swear i tried having the force string conversion after the field name, but may be I didnt. I still dont understand this. i though .format was just a "smart" way to align and print. Does the .format mean that every object being passed into it has its own .format that is being invoked?
@newkid101: yes, formatting is delegated to the __format__ method on the object.

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.