I have to read a data as records in plain text. I use open(“filename”).read() to get the data from a text file. The format is as given below:
Name 1 Address : Full Address Phone No : 3333333333 Email : email.address Mobile No : 9999999999 NEXT Name 2 Address : Full Address Email : email.address Mobile No : 9999999999 NEXT Name 2 Address : Full Address Phone No : 3333333333 Email : email.address Mobile No : 9999999999 NEXT
I am using ‘re.findall()’ to get the details into a string separated with “\t” so that I can paste it into an excel sheet. The format is as below:
NAME |ADDRESS |PHONE NO |EMAIL |MOBILE NO
So the data should be framed into a string in the following format:
dataline = “Name1\tFull Address\t3333333333\temail.address\t9999999999\n”
The problem is that, some may have Phone No, but some may not. My regex fails to get entire data when one of the field is missing as shown in my second record in the example above. It works for the first and third records but fails for the second record. I prefer to get a blank tuple or list if it is not present. Since the data is huge, the manual check is not possible. Just curious if it can be achieved through Regex.