简体   繁体   中英

double quote string python

I have a string of the following form

'"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'

I need to tease out the sub-strings in double quotes, so '08770' and 'tykjkj'.

How should I do it in python?

If you want to use regex:

>>> import re
>>> re.findall(r'""(.*?)""', '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"')
['O8770', 'tykjkj']

Obviously from the many answers, there are a lot of ways to do this. The trick is to "clean up" or "map" the initial string into something that can be readily separated, for example by the string split() method.

>>> s = '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'
>>> s.replace('"', '\t').split()
['asfdfdfds', 'O8770', 'asdsadjieere', 'tykjkj', 'ldkflskfd']

The choice of how to map the original string is dependent on what kind of strings you might get. Will they all have balanced quotes? What kind of whitespace or other characters might they include? How might real-world data be dirty / inconsistent with your basic parsing assumptions? Because every way to can try to parse / split the string is dependent on some assumptions.

This works from the command line interpreter.

s = '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'
s.split('\"')

.

result:
['', 'asfdfdfds', '', 'O8770', '', 'asdsadjieere', '', 'tykjkj', '', 'ldkflskfd', '']
mystring = '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'
mystring.strip('"').split('""')

Using the string.split function with argument " gives you the substrings.

'"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'.split('"')

result:

['', 'asfdfdfds', '', 'O8770', '', 'asdsadjieere', '', 'tykjkj', '', 'ldkflskfd', '']
vals = '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'.split('\"')
print [val for val in vals if val]

You could use a regular expression

import re
string = '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'
print re.findall('".+?"', string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM