-1

I need to write a part of the program which will work with a string, find in it some substrings and copy they to dictionary. Example of the string:

thestring ='\
#: somethings\nchars0 "substr0"\nchars1 "substr1"\n\n\
#: something\nchars0 "substr2"\nchars1 "substr3"'

so it have current substrings 'chars0' and 'chars1' which I know and some random substrings like 'substr0'...

But it is a small problem - on the start of the string anothoer structure:

'chars0 ""\nchars1 ""\n"words\\n"\n"else words\\n"\n ... '

and then structured string like above.

I know only number of this substrings with words, but I'm not interested in this subtrings, I need substrings only from structured part of the string.

If print the string, we get this:

chars0 ""
chars1 ""
"some words\n"
"else words\n"
... 

#: something
chars0 "subtring"
chars1 "else substring"

...

How can I optimize search of it? (I think that it is good to try find() string method or rfind() because of end of the string is structured)

  • What do you mean by "structured part of the string?" – Robert Harvey May 10 '16 at 5:37
  • I mean this: '#: somethings\nchars0 "substr0"\nchars1 "substr1"\n\n#: something\nchars0 "substr2"\nchars1 "substr3"' – Illia Ananich May 10 '16 at 5:52
0

A very basic way to parse this string might be

thestring ='\
#: somethings\nchars0 "substr0"\nchars1 "substr1"\n\n\
#: something\nchars0 "substr2"\nchars1 "substr3"'

for token in thestring.split('\n'):   
  if "#" in token:
    handler.heading(token)    
  elif "" == token:
    handler.section(token)
  else:
    handler.body(token)

print handler.dictOfDicts

This will print either

{'#: something': {'chars0 ': 'substr2', 'chars1 ': 'substr3'},
'#: somethings': {'chars0 ': 'substr0', 'chars1 ': 'substr1'}}

or

heading: #: somethings
key: chars0
value: substr0
key: chars1
value: substr1
section:
heading: #: something
key: chars0
value: substr2
key: chars1
value: substr3

depending on if handler is set to StringStructure() or StringPrinter(), respectively. Where

class StringStructure:
  dictOfDicts = {}  
  currentHeading = ''
  def heading( self, token ):
    self.currentHeading = token
    self.dictOfDicts[self.currentHeading] = {}
  def section( self, token ):
    pass
  def body( self, token ):
    key, value, junk = token.split('"')
    self.dictOfDicts[self.currentHeading][key] = value    

class StringPrinter:
  dictOfDicts = ''
  def heading( self, token ):
    print 'heading: ' + token
  def section( self, token ):
    print 'section: ' + token
  def body( self, token ):
    key, value, junk = token.split('"')
    print 'key: ' + key
    print 'value: ' + value

This is brittle because it assumes you will always have two items in the body, two levels of keys, and won't produce the most meaningful error messages when the format of your string is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.