how to extract string inside single quotes using python script

坚强是说给别人听的谎言 提交于 2019-11-28 10:38:27

问题


Have a set of string as follows

text:u'MUC-EC-099_SC-Memory-01_TC-25'
text:u'MUC-EC-099_SC-Memory-01_TC-26'
text:u'MUC-EC-099_SC-Memory-01_TC-27'

These data i have extracted from a Xls file and converted to string, now i have to Extract data which is inside single quotes and put them in a list.

expecting output like

[MUC-EC-099_SC-Memory-01_TC-25, MUC-EC-099_SC-Memory-01_TC-26,MUC-EC-099_SC-Memory-01_TC-27]

Thanks in advance.


回答1:


Use re.findall:

>>> import re
>>> strs = """text:u'MUC-EC-099_SC-Memory-01_TC-25'
text:u'MUC-EC-099_SC-Memory-01_TC-26'
text:u'MUC-EC-099_SC-Memory-01_TC-27'"""
>>> re.findall(r"'(.*?)'", strs, re.DOTALL)
['MUC-EC-099_SC-Memory-01_TC-25',
 'MUC-EC-099_SC-Memory-01_TC-26',
 'MUC-EC-099_SC-Memory-01_TC-27'
]



回答2:


You can use the following expression:

(?<=')[^']+(?=')

This matches zero or more characters that are not ' which are enclosed between ' and '.

Python Code:

quoted = re.compile("(?<=')[^']+(?=')")
for value in quoted.findall(str(row[1])):
    i.append(value)
    print i



回答3:


That text: prefix seems a little familiar. Are you using xlrd to extract it? In that case, the reason you have the prefix is because you're getting the wrapped Cell object, not the value in the cell. For example, I think you're doing something like

>>> sheet.cell(2,2)
number:4.0
>>> sheet.cell(3,3)
text:u'C'

To get the unwrapped object, use .value:

>>> sheet.cell(3,3).value
u'C'

(Remember that the u here is simply telling you the string is unicode; it's not a problem.)



来源:https://stackoverflow.com/questions/19449709/how-to-extract-string-inside-single-quotes-using-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!