7

I am relativly new to coding with Python.

I recently set up a gps logging device with a raspberry pi and I want to make my log file look cleaner.

My current code for logging is:

logging.info('Altitude:')
logging.info(gpsd.fix.altitude)

It logs:

INFO:root:Altitude:
INFO:root:80

What I want to see in the log is:

Altitude: 80

I tried to do this with my limited knowledge with python, but it only resulted in failure.

Thanks! Also, any other tips for cleaning up the log file?

2
  • 1
    Use .format() Commented Oct 28, 2013 at 6:01
  • 1
    What did you try? What error did you get when you tried it? Commented Oct 28, 2013 at 6:05

4 Answers 4

10

In Python >=3.6 you can do this :

logging.info(f"Altitude: {gpsd.fix.altitude}")

By adding the "f" at the beginning of the string the value between brackets will be interpreted as a variable and will be replaced by its value.

Sign up to request clarification or add additional context in comments.

Comments

5

If altitude is a decimal then

logging.info('Altitude: %d' % gpsd.fix.altitude)

will do it, there are several other ways to achieve the same thing though as I'm sure others can present!

1 Comment

@user2799617 That's a ridiculous reason.
4
logging.info('{}:{}'.format("Altitude", gpsd.fix.altitude)

You can use format method. Have a look at the examples to understand format better.

Example:

print '{}:{}'.format("Altitude", 80)

Output

Altitude:80

Comments

2

Try:

logging.info('Altitude:%s' % gpsd.fix.altitude)

1 Comment

The %s will stringify anything you pass it, so calling str is redundant.

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.