N
N
nevro2015-08-26 11:25:30
Python
nevro, 2015-08-26 11:25:30

How to parse a string and build a tree of product and service categories?

Scrapy and other options are great, but not suitable for self-education and pumping logic.
For example, you need to parse a string and build a tree of categories of goods and services (from the past Yandex competition for android development): https://money.yandex.ru/api/categories-list
piece: [{"title":"Phone" ,"subs":[{"id":157291,"title":"Mobile communication"},
Here the information is structured, not just an array. And the same is said about the tree. So,
1. first you need to choose a data type that is suitable for storing information in such a structure? For such a structure as in the condition, is it better to take a dict, or write your own?
2. In general, the algorithm looks like cycles of passing through the string and checking each character for all these brackets,
3. Does python use regular expressions for such passes and comparisons? I just started to get interested in the language - I managed to find out only about split in the string type.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaliy Orlov, 2015-08-26
@nevro

I will write in general, because this is suitable for any language
The algorithm of the parser is quite simple, and for the most part is divided into 3 stages:
1) Get data
2) Parse data
3) Save data
1) In order to get data, you need to study the stack of functions for working with the network. You can google for this query "How to download a web page" + your PL (PHP, Java, Python, etc.). Here you have to write a function to which you will pass a url to the input, and you will receive data (html, json, xml, etc.)
2) You can parse the data either using ready-made libraries for the desired format, or using regular expressions, or using string functions. Here the query "String functions" + your PL or "regular expressions" + your PL will come to the rescue. At this stage, you must write a function that receives data as input, and at the output you get a pre-approved structure.
3) You can save data in the required structure to files or to the database. Again in Google with the request "Working with the database" + your PL, or "Working with files" + your PL. Here your task is to write a function that receives a pre-approved structure as input, and at the output you get the result "saved data"
Well, that's all. You go to Google, study the materiel and write a parser.
If you are more interested in parsing algorithms, then I advise you to look at the code of "calculators" on your PL, you can google it on request "calculator with brackets", "Parse and evaluate expressions". This is a classic problem, parsing your piece will be very similar.

O
olexande, 2015-08-26
@olexande

For storage, perhaps trees will help ...
habrahabr.ru/post/112421

N
nonlux, 2015-08-26
@nonlux

Read the gang of four)
For example , an interpreter
Well, and much more from them, will allow you to create a concise parser.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question