Skip to content Skip to sidebar Skip to footer

How To Print Specific Strings In Python?

I have the follwing strings in a file: 736.199070736: LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 0x0075007f, 0x005500dd, 0x000a00f5) from these i want to take data from '(' to

Solution 1:

If you want it a single line:

''.join(''.join(text.splitlines()).partition('(')[1:])

Demo:

>>>text = '''\...736.199070736:  LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, ... 0x0075007f, ... 0x005500dd,  ... 0x000a00f5)...'''>>>''.join(''.join(text.splitlines()).partition('(')[1:])
'(0,  0x0075007f,  0x005500dd,   0x000a00f5)'

Bonus feature: If you pass this to ast.literal_eval() you get a Python structure with integers instead:

>>>from ast import literal_eval>>>literal_eval(''.join(''.join(text.splitlines()).partition('(')[1:]))
(0, 7667839, 5570781, 655605)

Solution 2:

You could use a regular expression:

import re

string = """736.199070736:  LOG_MOD_L0_RECEIVE_TXBRP_CONTROL(0, 
 0x0075007f, 
 0x005500dd,  
 0x000a00f5)"""

result = re.search(r'\(.*\)', string) # matches anything between parenthesis

result.group()

'(0, 0x0075007f, 0x005500dd,  0x000a00f5)'

That gives you the data as a string.

Post a Comment for "How To Print Specific Strings In Python?"