WaddyBox
WaddyBox

Reputation: 73

How would I print all the instances when the "$" shows up?

I have this string and I'm basically trying to get the numbers after the "$" shows up. For example, I would want an output like:

>>> 100, 654, 123, 111.654

The variable and string:

file = """| $100 on the first line
          | $654 on the second line
          | $123 on the third line
          | $111.654 on the fourth line"""

And as of right now, I have this bit of code that I think helps me separate the numbers. But I can't figure out why it's only separating the fourth line. It only prints out 111.654

txt = io.StringIO(file).getvalue()
idx = txt.rfind('$')

print(txt[idx+1:].split()[0])

Is there an easier way to do this or am I just forgetting something?

Upvotes: 0

Views: 64

Answers (5)

hexbioc
hexbioc

Reputation: 1606

Regular expressions yay:

import re

matches = re.findall(r'\$(\d+\.\d+|\d+)', file)

Finds all integer and float amounts, ensures trailing '.' fullstops are not incorrectly captured.

Upvotes: 2

Jamie Deith
Jamie Deith

Reputation: 714

How about this?

re.findall('\$(\d+\.?\d*)', file)

# ['100', '654', '123', '111.654']

The regex looks for the dollar sign \$ then grabs the maximum sized group available () containing one or more digits \d+ and zero or one decimal points \.? and zero or more digits \d* after that.

Upvotes: 0

jjfaress
jjfaress

Reputation: 47

Your whole sequence appears to be a single string. Try using the split function to break it into separate lines. Then, I believe you need to iterate through the entire list, searching for $ at each iteration. I'm not the most fluent in python, but maybe something like this:

for i in txt.split('\n'):
    idx=txt.rfind('$')
    print(txt[idx+1].split()[0])

Upvotes: 0

Miracle Max
Miracle Max

Reputation: 1

This should do it! For every character in txt: if it is '$' then continue until you find a space.

print(*[txt[i+1: i + txt[i:].find(' ')] for i in range(0, len(txt)) if txt[i]=='$'])

Output:

100 654 123 111.654

Upvotes: 0

Prune
Prune

Reputation: 77880

Your code finds only the last $ because that's exactly what you programmed it to do. You take the entire input, find the last $, and then split the rest of the string. This specifically ignores any other $ in the input.

You cite "line" as if it's a unit of your program, but you've done nothing to iterate through lines. I recommend that you quit fiddling with io and simply use standard file operations. You find this in any tutorial on Python files.

In the meantime, here's how you handle the input you have:

by_lines = txt.split('\n')    # Split in newline characters
for line in by_lines:
    idx = line.rfind('$')
    print(line[idx+1:].split()[0])

Output:

100
654
123
111.654

Does that get you moving?

Upvotes: 2

Related Questions