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.

david_p
1,8031 gold badge20 silver badges34 bronze badges
asked Feb 16, 2016 at 19:13
1
  • This is a pure Python question that I can remember researching and finding a solution for at Stack Overflow. Commented Feb 16, 2016 at 20:06

1 Answer 1

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.

answered Feb 16, 2016 at 19:28
1
  • 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])))' Commented Feb 16, 2016 at 21:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.