3

I set up a script to print some statistics about line segments. The "logfile" is basically the same as "print", but to a textfile instead of on screen only.

LogFile("Feature {0} has a length of {1} meters at coordinates {2}".format(row[0], row[1], row[2]))

This will give me an output like:

Feature 1 has a length of 346.239783695 meters at coordinates (470765.69285000017, 6313169.6829)

I want to drop the decimals (i.e., turn the values into integers for reporting purposes only) and just have

Feature 1 has a length of 346 meters at coordinates (470765, 6313169)

How do I alter the format within the script to accomplish that? I assume it will be super easy and something I should know, but I've been out of the Arc/Python world for a few years.

  • This is a pure Python question that I can remember researching and finding a solution for at Stack Overflow. – PolyGeo Feb 16 '16 at 20:06
3

This can be accomplished by taking full advantage of the format function you're already using. In its basic form (e.g. '{0}'.format(var)), the value returned is equivalent to str(var). However, by adding stuff inside the curly braces, you can tweak the formatting quite extensively -- see PEP 3101.

For your specific example, try this:

LogFile("Feature {0} has a length of {1:.0f} meters at coordinates ({2:.0f}, {3:.0f})".format(row[0], row[1], row[2][0], row[2][1]))

I had to separate the coordinates tuple into its individual components, as the float formatting doesn't work on tuples directly.

  • 1
    Thanks for that. I figured it was something simple. The tuple thing got me for a bit. The following also seems to work 'LogFile("Feature {0} has a length of {1} meters at coordinates ({2}, {3})".format(int(row[0]), int(row[1]), int(row[2][0]), int(row[2][1])))' – Jeff C Feb 16 '16 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.