ganesh reddy
ganesh reddy

Reputation: 1892

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?

Upvotes: 0

Views: 150

Answers (7)

max
max

Reputation: 46

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

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

result:

['', 'asfdfdfds', '', 'O8770', '', 'asdsadjieere', '', 'tykjkj', '', 'ldkflskfd', '']

Upvotes: 0

MLadbrook
MLadbrook

Reputation: 75

You could use a regular expression

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

Upvotes: 0

Jonathan Eunice
Jonathan Eunice

Reputation: 22483

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.

Upvotes: 2

Saksham Varma
Saksham Varma

Reputation: 2140

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

Upvotes: 0

Ben
Ben

Reputation: 6777

If you want to use regex:

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

Upvotes: 2

adapt-dev
adapt-dev

Reputation: 1768

This works from the command line interpreter.

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

.

result:
['', 'asfdfdfds', '', 'O8770', '', 'asdsadjieere', '', 'tykjkj', '', 'ldkflskfd', '']

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49330

mystring = '"asfdfdfds""O8770""asdsadjieere""tykjkj""ldkflskfd"'
mystring.strip('"').split('""')

Upvotes: 0

Related Questions