0

I have the same problem like this guy:

partition string in python and get value of last segment after colon

Mine is like:

IP-Adress: 1.1.1.1 Device: Fritzbox Serialnumber: 123456789

I want to only get the Device so my Output should look like: "Fritzbox" i dont need anything else.

result = mystring.rpartition(':')[2]

is this possible with this kinda code? If yes what do i have to change to cut the rest off?

4 Answers4

2

You can use re.split here and use the result to create a dictionary - that way you can access any keys you want, eg:

import re

text = 'IP-Adress: 1.1.1.1 Device: Fritzbox Serialnumber: 123456789 Description: something or other here test: 5'
split = re.split(r'\s*(\S+):\s+', text)
data = dict(zip(split[1::2], split[2::2]))

This gives you a data of:

{'IP-Adress': '1.1.1.1',
 'Device': 'Fritzbox',
 'Serialnumber': '123456789',
 'Description': 'something or other here',
 'test': '5'}

Then access that as you want, eg:

device = data.get('Device', '***No Device Found???***')

This way you get access to all key/value pairs should you ever want them, it doesn't rely on any ordering of keys nor their actual presence in your text.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

Assuming 'Device:' is always present, the following Regular expression should work for you:

s = 'IP-Adress: 1.1.1.1 Device: Fritzbox Serialnumber: 123456789'

import re
re.search(r'Device:\s*(\w+)', s).group(1)
# 'Fritzbox'

Or if you prefer string methods, you could do something like:

s.split(':')[-2].strip().split()[0]
# 'Fritzbox'
yatu
  • 86,083
  • 12
  • 84
  • 139
  • How would it look like when i use an array? Cause im saving the output in an list i get when getting the sysinfo from my router – Dark Shadow Mar 22 '19 at 10:00
  • So you have a list of strings? You'd have to iterate over the list and find the match for every string. Hard to tell exactly what you want without a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – yatu Mar 22 '19 at 10:02
  • Like a typical sysinfo just in an array my problem is that he saves all the sysinfo into the string. The Array looks like: '\r\n\r\nDevice: Fritzbox 7490 C\r\nSerial-Number: 289347290 and so on but the problem is that its saved as one value – Dark Shadow Mar 22 '19 at 10:22
  • Please update the question with a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) @DarkShadow. Or ask a new question for this as you are really moving from one question to another here – yatu Mar 22 '19 at 10:25
0

Assuming Device: and Serialnumber are always present:

s = 'IP-Adress: 1.1.1.1 Device: Fritzbox Serialnumber: 123456789'

def GetInBetween(s, st, ed):
  return (s.split(st))[1].split(ed)[0]

print(GetInBetween(s, 'Device:', 'Serialnumber').strip())

OUTPUT:

Fritzbox

EDIT:

If you have a list of those strings:

sList = ['IP-Adress: 1.2.2.2 Device: Fritzbox Serialnumber: 123456789',
        'IP-Adress: 1.3.4.3 Device: Macin Serialnumber: 123456789',
        'IP-Adress: 1.123.12.11 Device: IBM Serialnumber: 123456789',
         ]

for elem in sList:
    print(GetInbetween(elem, 'Device:', 'Serialnumber').strip())

OR

Using list comprehension:

print([GetInbetween(x, 'Device:', 'Serialnumber').strip() for x in sList])

OUTPUT:

['Fritzbox', 'Macin', 'IBM']
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • The elem thing is not working, He says its not a string cause you defined 3 str in the getinbetween function – Dark Shadow Mar 22 '19 at 10:34
  • @DarkShadow how is it not working for you? Did you get any errors? The posted code is tested. – DirtyBit Mar 22 '19 at 10:35
  • Type 'Tuple[str, str, str]' doesn't have expected attribute 'split' less... (Strg+F1) i get this error here: for elem in thisdict: print(GetInBetween(elem, 'Device:', 'Serialnumber').strip()) – Dark Shadow Mar 22 '19 at 10:40
  • @DarkShadow Oh, that is a tuple not a str. the `split` works on str. Could post a sample of your Input data? – DirtyBit Mar 22 '19 at 10:41
  • The problem was that i had [] and not {} for the list. The main problem still presists: , line 19, in print(str(GetInBetween(elem, 'Device:', 'Serialnumber')).strip()) line 15, in GetInBetween return (s.split(st))[1].split(ed)[0] AttributeError: 'int' object has no attribute 'split' – Dark Shadow Mar 22 '19 at 11:03
  • I guess the error is cause in the function "getinbetween" the function parameters are str,str,str and not int,str,str cause elem is int an not str but how can we change this? with a simple typecast? – Dark Shadow Mar 22 '19 at 11:04
  • @DarkShadow Again, it would be very helpful if you could provide your sample data be it within `{}` or `[]` and to cast a `str` to an `int` i.e. `s = '30'` `x = int(s)` would do it – DirtyBit Mar 22 '19 at 11:06
0

using pygrok python package we can extract data from the string in a structured format.

A Python library to parse strings and extract information from structured/unstructured data.

https://pypi.org/project/pygrok/

pip install pygrok

from pygrok import Grok
text = 'IP-Adress: 1.1.1.1 Device: Fritzbox Serialnumber: 123456789'
pattern = 'IP-Adress: 1.1.1.1 Device: %{WORD:device} Serialnumber: 123456789'
grok = Grok(pattern)
print (grok)
#output
{
  "device": [
   ["Fritzbox"]
]
}
vinodsesetti
  • 368
  • 2
  • 6