user8560167
user8560167

Reputation:

extracting strings between two characters in bytes text file ,Python

my data is coming from subprocess and on the output i am receiving :

sample:

helo, aawe.adam , by the w,: 12.1:\r\n . heesa1.\r\n,b'asdasd',nme.AAAA.\r\n

type=<class 'bytes'>

i want to strip it in one line and then extract only barts between two characters, in this example . (dot)

expected result:

adam , by the w,: 12
1:
heesa1
,b'asdasd',nme
AAAA

I've also tried this method: Extracting text between two strings

but i am receiving errors : TypeError: cannot use a string pattern on a bytes-like object

thanks in advice `

Upvotes: 0

Views: 333

Answers (2)

Sunil Kumar
Sunil Kumar

Reputation: 206

>>> o = b"helo, aawe.adam , by the w,: 12.1:\r\n . heesa1.\r\n,b'asdasd',nme.AAAA.\r\n"
>>> o.split(b".")
[b'helo, aawe', b'adam , by the w,: 12', b'1:\r\n ', b' heesa1', b"\r\n,b'asdasd',nme", b'AAAA', b'\r\n']

string split should help you by ignoring first and last item in the splited list.

Upvotes: 0

Nick
Nick

Reputation: 3845

You need to decode the bytes for this to work. Try this:

output = b'helo, aawe.adam , by the w,: 12.1:\r\n . heesa1.\r\n,b'asdasd',nme.AAAA.\r\n'
str = output.decode("utf-8")

Then you can try to extract the data as you have before.

Upvotes: 1

Related Questions