0

This is my code :

import re
a = 'CUSTOMER:SHA\n SERVICE:XX\n MOBILE:12345\n'
match= re.findall(r':[\w]+', a)
print  match

The output is : [':SHA', ':XX', ':12345']

I need to eliminate the colon and print the values in newline. How do i do it ?

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • Hey Vidhya, Have a look at this http://stackoverflow.com/questions/18304835/parsing-a-text-file-into-a-list-in-python ___ http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python?rq=1 – Rahul Mar 03 '16 at 06:22
  • 1
    @RahulDambare I need to eliminate the ' : ' in the output..How does the link match my question? This is not json ... – Vidhya Saraswathi Mar 03 '16 at 06:26
  • If you understand regex, then use a group like `(\w+)`. The `[ ]` aren't necessary for your use since there is only one character the brackets – OneCricketeer Mar 03 '16 at 06:30

4 Answers4

1

just use this regex

match= re.findall(r':(\w+)', a)

see this

to print on new line you can use for for example:

import re
a = 'CUSTOMER:SHA\n SERVICE:XX\n MOBILE:12345\n'
match= re.findall(r':(\w+)', a)

for i in match:
    print(i)

which produce this output:

SHA
XX
12345

sunny
  • 178
  • 3
  • 12
0

You can iterate match to get each value. In each value you can then replace the colon with empty string and print it out. Here is the code you can add to what you already have to accomplish this.

for value in match:
   new = value.replace(":", "")
   print new
0

Try this,

import re
a = 'CUSTOMER:SHA\n SERVICE:XX\n MOBILE:12345\n'
for item in re.findall(r':[\w]+', a):
  print item[1:]
Kajal
  • 709
  • 8
  • 27
0

How about this:

>>> re.findall(r':(\w+)',a)
['SHA', 'XX', '12345']
Iron Fist
  • 10,739
  • 2
  • 18
  • 34