-2

I am currently parsing this huge rpt file. In each item there is a value in parentheses. For example, "item_number_one(3.14)". How could I extract that 3.14 using the split function in python? Or is there another way to do that?

#Splits all items by comma
items = line.split(',')

#splits items within comma, just gives name
name_only = [i.split('_')[0] for i in items]
# print(name_only)

#splits items within comma, just gives full name
full_name= [i.split('(')[0] for i in items]
# print(full_Name)

#splits items within comma, just gives value in parentheses
parenth_value = [i.split('0-9')[0] for i in items]
# parenth_value = [int(s) for s in items.split() if s.isdigit()]
print(parenth_value)

parenth_value = [i.split('0-9')[0] for i in items]
CarlosX2X
  • 193
  • 1
  • 1
  • 9
  • So why are you splitting by comma? Please give [mcve]. – Austin Jun 18 '19 at 16:16
  • You can probably come up with a way to split using the parenthesis but regex would be easier in this case. I think you can use the pattern: `(?<=\()(\d|\.)+(?=\))` or maybe just the `(\d|\.)+`. – pault Jun 18 '19 at 16:16
  • Since you are using RPT this answer might be helpful: https://stackoverflow.com/a/48953650/1913726 so you don't have to manually split the lines. – dmmfll Jun 18 '19 at 16:19
  • You can also try [this](https://stackoverflow.com/questions/4894069/regular-expression-to-return-text-between-parenthesis) – Devesh Kumar Singh Jun 18 '19 at 16:21
  • ["Can Someone Help Me?" is not a valid SO question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). This usually suggests that what you need is time with a local tutor or walk through a tutorial, rather than Stack Overflow. The code you posted shows little attempt to solve the given problem. – Prune Jun 18 '19 at 16:39

4 Answers4

0

for a more general way of extracting numbers from strings, you should read about Regular Expressions.

for this very specific case, you can split by ( and then by ) to get the value in between them.

like this:

line = "item_number_one(3.14)"
num = line.split('(')[1].split(')')[0]
print(num)
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
0

You could simply find starting index of parentheses and ending parentheses, and get the area between them:

start_paren = line.index('(')
end_paren = line.index(')')
item = line[start_paren + 1:end_paren]
# item = '3.14'

Alternatively, you could use regex, which offers an arguably more elegant solution:

import re
...
# anything can come before the parentheses, anything can come afterwards.
# We have to escape the parentheses and put a group inside them
#  (this is notated with its own parentheses inside the pair that is escaped)
item = re.match(r'.*\(([0-9.-]*)\).*', line).group(1)
# item = '3.14'
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

can use regex and do something like below;

import re
sentence = "item_number_one(3.14)"
re.findall(r'\d.+', sentence)
ysf
  • 4,634
  • 3
  • 27
  • 29
0

You could get the integer value by using the following regular expression:

import re

text = 'item_number_one(3.14)'

re.findall(r'\d.\d+', text)

o/p: ['3.14']

Explanation:

"\d" - Matches any decimal digit; this is equivalent to the class [0-9].

"+" - one or more integers

In the same way you can parse the rpt file and split the lines and fetch the value present in the parentheses .

NPC
  • 80
  • 6