0

I am communicating to serial ports via python. I passed an AT command to list the phone directory. Now I need to fetch the phone number I received. How do I fetch that particular number via python.

+CMGR: "REC READ","+911234567890",,"13/05/31,10:00:14+22"
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Kushal Shah
  • 165
  • 1
  • 13

2 Answers2

1

something like this?

>>> import re
>>> strs = '+CMGR: "REC READ","+911234567890",,"13/05/31,10:00:14+22'
>>> re.search(r'"(\+91\d+)"', strs).group(1)
'+911234567890'
>>> 
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • import serial from curses import ascii import re ser=serial.Serial('/dev/ttyUSB0', 9600, timeout=3) response=ser.write('AT+CMGR=3\r\n') re.search(r'"(\+91\d+)"', response).group(1) This isn't working either – Kushal Shah Jun 04 '13 at 04:51
1

If the number is always the nth field, then:

s = '+CMGR: "REC READ","+911234567890",,"13/05/31,10:00:14+22"'
import csv
print next(csv.reader([s]))[1]
# +911234567890
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • import serial from curses import ascii ser=serial.Serial('/dev/ttyUSB0', 9600, timeout=3) response=ser.write('AT+CMGR=3\r\n') import csv print next(csv.reader([response]))[1] This isn't working. – Kushal Shah Jun 04 '13 at 04:49