tokenizing and parsing with python -


i don't have code show because have no idea on how start. current target @ least able create tokens file contains data eg:

file.txt

name : sid data : lazy developer  %description  packaging file   %install  enter location install package. 

and python code should able create tokens file , when required print data based on input.

if getdata() function

getdata('name') should output "sid" getdata('description') should give text below it.

to retrieve data file.txt:

data = {} open('file.txt', 'r') f: # opens file     line in f: # reads line line         key, value = line.split(' : ') # retrieves key , value         data[key.lower()] = value.rstrip() # key lower case , removes end-of-line '\n' 

then, data['name'] returns 'sid'.

edit: question has been updated new solution:

data = {} open('file.txt', 'r') f:     header, *descriptions = f.read().split('\n\n')     line in header.split('\n'):         key, value = line.split(' : ')         data[key.lower()] = value.rstrip()     description in descriptions:         key, value = description.split('\n', 1)         data[key[1:]] = value print(data) 

you might have adapt if there whitespaces between lines or @ end of keys...

a shorter way might use regex , method re.group().


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -