0

I am reading in a string from a file which has the format $ 235.0 M. I need to convert this to a number and save the letter M somewhere else. Any pointers on how to strip off the $ and M?

Thank you.

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by oaktrees
0

Hint:

data='$ 235.0 M'
print data.lstrip('$ ')
print data.rstrip(' M')

Edited by pyTony: n/a

0

Here is an example. Remember that after reading from a text-file, (if you read the whole line) you may get newline characters ("\n"). Also, you need to remove the spaces.

n = '$ 235.0 M'            #Example string
n = n.strip('$M ')         #Cut off the chars - including spaces!
n = float(n)               #Must convert to int because of the '.0' ('235.0')
##################
print n * 2                #Just to test if it works

Enjoy

0

Sorry Tony, I got distracted after I started writing a reply and your post didn't show up until I had added mine!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.