Reputation: 159
Say I have a string that looks like this "53.4 -63.2 433.2" three separate numbers. How do I get the middle one only and so I dont ignore a minus if the number is negative?
Ive managed to gather to use (\d+) to group the numbers and to grab more than one..But I dont know what to add? IVe looked at similar questions but I guess I could not find a similar enough case to what I am trying to do
Upvotes: 0
Views: 908
Reputation: 46759
If you are reading from a file, something like the following could be used:
import csv
reader = csv.reader(open("numbers.txt", "rb"), delimiter=" ")
for row in reader:
if len(row) == 3:
print row[1]
As mentioned, use of a regular expression seems like overkill.
Upvotes: 0
Reputation: 174706
You may use re
module.
re.search(r'(?s)^\S+\s+(\S+)', stri).group(1)
Example:
>>> import re
>>> s = ["""53.4
-63.2 433.2""", """53.4 -63.2 433.2""", """53.4 -63.2
433.2"""]
>>> [re.search(r'(?s)^\S+\s+(\S+)', i).group(1) for i in s]
['-63.2', '-63.2', '-63.2']
Upvotes: 0
Reputation: 24133
def middle_floats(filename):
with open(filename) as lines:
for line in lines:
yield line.split()[1]
Upvotes: 0
Reputation: 8593
Aussuming your data has the form of the sample multiline_string
you can iterate through the string line by line by providing an iterator and split()
each line into several groups. If there are exact three groups take the middle one and cast the string into a float. Pass the extracted number (which is a float) to abs()
to get the absolute value:
#!/usr/bin/env python3
# coding: utf-8
multiline_string = """
123 -456 789
101 102 103
35.7 -43.8 21.8
10. 10.2 10.3
"""
for line in iter(multiline_string.splitlines()):
groups = line.split()
if len(groups) == 3:
number = abs(float(line.split()[1]))
print(number)
Upvotes: 0
Reputation: 1319
Contrary to other response, I am going to give you the regular expression way that will only match for line of three number.
I will do it this way : catch three number which could have an optional negate (?-) and which could have optional dot with digit after (.?)\d+, I will put a group mark on the second to retrieve it (-?\d+.?\d+). And I will able those number to be separated by a random number of space \s+.
import re
m = re.search(r"-?\d+\.?\d+\s+(-?\d+\.?\d+)\s+-?\d+\.?\d+", "53.4 -63.2 433.2")
print(re.group(1))
If you have difficulty understanding my regexp, you should copy it in https://regex101.com/#python : it will give an explanation for everything.
You should also take a look at Python re module documentation : https://docs.python.org/2/library/re.html
Upvotes: 2
Reputation:
I hope this will help:
str_num = "53.4 -63.2 433.2"
list_num = str_num.split("\t")
# Now print the middle item by using lenght of list divided by 2.
middle = list_num[len(list_num)/2]
This will work for list of any lenght. So, you don't have to hardcode the index.
Upvotes: 0
Reputation: 581
If there will always be 3 numbers per line, separated with spaces, then regex is a bit overkill. Something like this should suffice:
numbers = []
for line in infile:
numbers.append(line.split()[1])
Upvotes: 0